Entries Tagged as 'java.io'
Change file or directory permission using java
The setReadOnly, setWritable,setReadable and setExecutable methods of the file class is used for changing the permissions on a file.
import java.io.File;
/**
* File class examples.
* Performs different permission operations on a file or directory.
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the source [...]
[Read more →]
Tags: java.io
Change the last modified date of a file or directory.
The setLastModified method of file class can be used for changing the last modified time of a directory or a file.
import java.io.File;
import java.util.Date;
/**
* File class examples
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the source file/directory
File file = new [...]
[Read more →]
Tags: java.io
Moving a file or directory in Java
The renameTo method of file class can be used for moving a file or directory to another destination.
import java.io.File;
/**
* File class examples
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the source file/directory
File file = new File(“d:\\temp\\test.txt”);
// Create the destination file/directory.
File destintation = [...]
[Read more →]
Tags: java.io
Renaming a file in Java
The renameTo method of file class can be used for renaming a file or directory.
import java.io.File;
/**
* File class examples
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the source file/directory
File file = new File(“d:\\temp\\test.txt”);
// Create the destination file/directory.
File destintation = new File(“d:\\temp\\test1.text”);
// Rename the file.
boolean [...]
[Read more →]
Tags: java.io
Creating a directory with sub directory
The mkdirs method of file class is used for creating a direcory and sub directories in Java
import java.io.File;
/**
* File class examples
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the directory
File file = new File(“d:\\temp\\dir\\subdir”);
// Create directory and sub directories.
boolean status = file.mkdirs();
System.out.println(“Directory creation [...]
[Read more →]
Tags: java.io
Create a directory using java
The mkdir method of file class is used for creating a new directory in java.
import java.io.File;
/**
* File class examples
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the directory
File file = new File(“d:\\temp\\dir”);
// Create directory
boolean status = file.mkdir();
System.out.println(“Directory creation status – “+status);
}
}
[Read more →]
Tags: java.io
Creating a FileFilter
We need to implement the FileFilter class for creating a file filter. The FileFilter interface contains only one method accept(File). This method is used for checking whether a file should be included or excluded.
import java.io.File;
import java.io.FilenameFilter;
/**
*
* A file filter.
*
*/
class TextFileFilter implements FileFilter {
/**
* Pattern would contain the [...]
[Read more →]
Tags: java.io
List all the contents of a directory according to a filter.
The list method of File class can be used for listing all the contents of a directory according to a filter.
import java.io.File;
import java.io.FilenameFilter;
/**
* File class examples
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the directory
File file = new [...]
[Read more →]
Tags: java.io
Writing a filename filter.
We need to implement the FilenameFilter class for writing a filename filter. The FilenameFilter interface contains only one method accept(File, String). This method is used for checking whether a file should be included.
import java.io.File;
import java.io.FilenameFilter;
/**
*
* A file filter.
*
*/
class FileFilter implements FilenameFilter{
/**
* Pattern would contain the extension [...]
[Read more →]
Tags: java.io
The list method of File class can be used for listing all the contents of a directory.
import java.io.File;
/**
* File class examples
*/
public class FileExample {
public static void main(String[] args){
// Create file object representing the directory
File file = new File(“d:\\temp”);
// List the contents of the directory.
String contents[] = file.list();
// Loop through the array and display [...]
[Read more →]
Tags: java.io