Sunday, September 25, 2011

How to extract a list of filtered files in Java?

Sometimes, we need to get a list of files exist within a directory as per our criteria like filename, file extension, last modified date or other attributes. It reduces the overhead to filter them after getting the list using our code. To achieve this, Java provides an interface - FileFilter which has a single method prototype as boolean accept(File file). To apply our filtering criteria, we have to override this method and if our logic returns true for a file then only the file will be included in the list otherwise not. Here is the sample code -
import java.io.*;
import java.util.*;

public class FilteredFileList {
    public static File[] getFilteredFileList(File folder, final String filenameFilter, Date lastModifiedDateFilter) {
        File[] listOfFiles = folder.listFiles(new FileFilter(){
                                public boolean accept(File file) {
                                    //file is the object against which we are applying our logic.
                                    //You can put your logic using filenameFilter and check whether
                                    //the file's name comes under your criteria or not.
                                    //To check against the lastmodified date you can use lastModifiedDateFilter.
                                    return true;
                                }});
        return listOfFiles;
    }
}

8 comments:

  1. Nice post you can also check these topics on tech guru and about many products.

    ReplyDelete
  2. Thank you a lot for giving everyone an extraordinarily nice opportunity to read from this blog.

    ReplyDelete
  3. Use a Filter to Retrieve a Specific Subset of Files in a Directory
    In Java, File.list() lists all the files in a directory. However, rather than listing all the files and then discarding unnecessary files in your code, this tip will show you how to use a filter to retrieve a specific subset of files.
    java.io.FilenameFilter is an interface which can be used to get only that set of files that match a specific criteria. Implement this interface to provide your logic of filtering:


    public class FileExtensionFilter implements FilenameFilter{
    private String ext="*";
    public FileExtensionFilter(String ext){
    this.ext = ext;
    }
    public boolean accept(File dir, String name){
    if (name.endsWith(ext))
    return true;
    return false;
    }
    }
    To use this filter in your code:

    File f = new File("C:\\sample");
    FileExtensionFilter filter = new FileExtensionFilter(".java");
    String[] contents = f.list(filter);
    This example returns only those files with a .java extension.


    http://www.ecotectanalysis.com/

    ReplyDelete
  4. Thanks for this great sharing........
    import java.io.File;
    import java.io.FileFilter;

    public class MainClass {

    public static void main(String[] args) {

    File cwd = new File(System.getProperty("user.dir"));
    File[] htmlFiles = cwd.listFiles(new HTMLFileFilter());
    for (int i = 0; i < htmlFiles.length; i++) {
    System.out.println(htmlFiles[i]);
    }
    }
    }

    class HTMLFileFilter implements FileFilter {

    public boolean accept(File pathname) {

    if (pathname.getName().endsWith(".html"))
    return true;
    if (pathname.getName().endsWith(".htm"))
    return true;
    return false;
    }
    }

    ReplyDelete
  5. Thank you a lot for giving everyone an extraordinarily nice opportunity to read from this blog.

    ReplyDelete
  6. Very helpful code for extract a list of filtered files in Java.

    ReplyDelete