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 extension of
* the file.
*/
private String pattern;
/**
* Initializes the pattern.
*
* Provide the file extension to filter.
*
* @param pattern
*/
public TextFileFilter(String pattern){
this.pattern = pattern;
}
public boolean accept(File pathname) {
return pathname.getName().endsWith(this.pattern);
}
}
}
To invoke the above FileFilter create an object of the TextFileFilter class like
TextFileFilter filter = new TextFileFilter("txt");
The argument to TextFileFilter will be the extension of the file we need to include.
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 File("d:\\temp");
// Create a FilenameFilter for filtering all the text files.
FileFilter filter = new FileFilter("txt");
// List the contents of the directory.
String contents[] = file.list(filter);
// Loop through the array and display the file/directory names.
for (int i = 0; i < contents.length; i++) {
System.out.println(contents[i]);
}
}
}
/**
*
* A file filter.
*
*/
class FileFilter implements FilenameFilter{
/**
* Pattern would contain the extension of
* the file.
*/
private String pattern;
/**
* Initializes the pattern.
*
* Provide the file extension to filter.
*
* @param pattern
*/
public FileFilter(String pattern){
this.pattern = pattern;
}
public boolean accept(File dir, String name) {
return name.endsWith(this.pattern);
}
}
The above examples filters out all the non text files from the directory. The list method returns an array of file names that matches the filter. But if we want an array of File objects instead of file names we should use listFiles method instead of list method.
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 of
* the file.
*/
private String pattern;
/**
* Initializes the pattern.
*
* Provide the file extension to filter.
*
* @param pattern
*/
public FileFilter(String pattern){
this.pattern = pattern;
}
public boolean accept(File dir, String name) {
return name.endsWith(this.pattern);
}
}
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 the file/directory names.
for (int i = 0; i < contents.length; i++) {
System.out.println(contents[i]);
}
}
}
Tags: java.io
Delete a file or directory when JVM terminates.
The deleteOnExit method of file class can be used for deleting a file or directory when JVM terminates.
package io;
import java.io.File;
/**
* File class examples
*
* Contains different operations of java.io.File class.
*/
public class FileExample {
public static void main(String[] args){
File file = new File("d:\\temp\\test1.txt");
// Delete the file or directory when JVM terminates
file.deleteOnExit();
}
}
Tags: java.io
Delete an existing file or directory
The delete method of file class can be used for deleting a file or directory. The delete method returns true if the file or directory was successfully deleted from the filesystem. If the file object represents a directory, the directory has to be empty for a successful deletion.
package io;
import java.io.File;
/**
* File class examples
*
* Contains different operations of java.io.File class.
*/
public class FileExample {
public static void main(String[] args){
File file = new File("d:\\temp\\test1.txt");
// Delete the file or directory
boolean isDeleted = file.delete();
System.out.println(isDeleted);
}
}
Tags: java.io
Creating new file
The createNewFile method of file class can be used for creating a new file. The createNewFile method returns true if the file does not exist and was successfully created an empty file.
package io;
import java.io.File;
import java.io.IOException;
public class FileExample {
public static void main(String[] args) throws IOException{
File file = new File("d:\\temp\\test1.txt");
// Create new file.
boolean isCreated = file.createNewFile();
System.out.println(isCreated);
}
}
Tags: java.io
Finds the size of a file.
The length method of file class can be used for finding the size of a file. If the file object represents a directory the length method returns zero.
package io;
import java.io.File;
public class FileExample {
public static void main(String[] args){
File file = new File("d:\\temp\\test.txt");
// Find the size of the file in bytes
long value = file.length();
// Convert the size in bytes to mega bytes.
double sizeInMB =(double) value / 1024 / 1024;
System.out.println(sizeInMB);
}
}
Tags: java.io
Finds the last modified date of a file or directory.
The lastModified method of file class can be used for finding the last modified date of a file or directory.
package io;
import java.io.File;
import java.util.Date;
public class FileExample {
public static void main(String[] args){
File file = new File("d:\\temp\\test.txt");
// Find the last modified date
long value = file.lastModified();
// Convert the value to date
Date lastModifiedDate = new Date(value);
System.out.println(lastModifiedDate);
}
}
Tags: java.io
Tests whether the file or directory is hidden or not.
The isHidden method of file class can be used for checking whether a file or directory is hidden or not.
package io;
import java.io.File;
public class FileExample {
public static void main(String[] args){
File file = new File("d:\\temp\\test.txt");
// Tests whether the file object is hidden or not.
boolean value = file.isHidden();
System.out.println(value);
}
}
Tags: java.io