Java File Search - Basics - How to search a directory for files based on name, type and size using the FileFilter interface?
Java File Search - Basics - How to search a directory for files based on name, type and size using the FileFilter interface?
This article is part of a tutorial.
Tutorial Index page - Java File Search
Program Requirements
To begin with lets define the requirements:
In a given directory list all the files
- that contains a given 'search text' in the file name and/or
- that contains a given 'search text' in the file contents and/or
- whose size is =, >, < the given 'search file size'
Background
Lets first look at the J2SE development kit and see what it offers out of the box. The first key primary class that we should be looking at is the File class itself.
java.io.File
According to the Java API documentation this class is a "An abstract representation of file and directory pathnames". Out of all the various useful functions this class provides, the methods that will be of interest to us are| File[] | listFiles(FileFilter filter) Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
| File[] | listFiles(FilenameFilter filter) Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
Both the above methods can be used to list files based on a filter that defines a search criteria - exactly what we are looking for! but which one to choose and whats the difference between these teo methods? By just looking at the method signature above, obviously the one and only difference between these two methods is the method argument. Thus by looking closer into these method arguments we can deceide on which method is the most appropriate one for our requirements.
method - listFiles(FileFilter filter)
FileFilter is a Java Interface which mandates a one and only method to be implemented which is:
| boolean | accept(File pathname) Tests whether or not the specified abstract pathname should be included in a pathname list. |
When th listFiles(FileFilter filter) method is called on a File object that represents a directory,
FileFilter fileFilter = new FileFilter() { public boolean accept(File pathname) { /** * Logic to filter files here */ return true; // or false } }; File dir = new File("c:/testfolder"); File[] searchResults = dir.listFiles(fileFilter);
for each file in that directory the accept method of the supplied FileFilter object will be called. If this method returns true for a file then that file will be added to the final list the listFiles will return and therefore it is in this 'accept(File pathname)' method where we will implement our logic to filter/search files.
As the accept method of the FileFilter interface supplies the abstract path of the files (as the parameter) using which we can read the file contents and its size, this FileFilter interface and hence the File.listFiles(FileFilter filter) method is suitable for meeting our requirement 3 - 'list files whose size is =, >, < the given 'search file size'
method - listFiles(FilenameFilter filter)
FilenameFilter is a Java Interface which mandates a one and only method to be implemented which is:
| boolean | accept(File dir, String name) Tests if a specified file should be included in a file list. |
When the listFiles(FilenameFilter filter) method is called on a File object that represents a directory,
FilenameFilter fileNameFilter = new FilenameFilter() { public boolean accept(File dir, String name) { /** * Logic to filter files here */ } }; File dir = new File("c:/testfolder"); File[] searchResults = dir.listFiles(fileNameFilter);
for each file in that directory the accept method of the supplied FilenameFilter object will be called. If this method returns true for a file then that file will be added to the final list the listFiles will return and therefore it is in this 'accept(File dir, String name)' method where we will implement our logic to filter/search files.
As the accept method of the FilenameFilter interface supplies the name of the files (as the parameter), this FilenameFilter interface and hence the File.listFiles(FilenameFilter filter) method is suitable for meeting our requirement 1 - 'that contains a given 'search text' in the file name' and requirement 2 - 'that contains a given 'search text' in the file contents'
Feedback or Questions?
We welcome feedback and questions and will try our best to attend to it as quickly as possible!
Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.
