Menu
Topics Index
...
`


Input/Output: Exploring java.io > File >
Siva Nookala - 06 Oct 2016
There is an alternative for list method discussed in Using FilenameFilter Interface In Java. list method it returns the names of the String array. But what if we want File objects. So in order to get File objects we use a method listFiles which returns File objects that match the specified criteria.

The signatures for listFiles() are shown below:
File[] listFiles()
File[] listFiles(FilenameFilter FFObj)
File[] listFiles(FileFilter FObj)
These methods return the file list as an array of File objects instead of strings.

The first method works just like the list method only difference it returns the File objects.

The second method it returns the File objects that ends with the specified extension. FilenameFilter interface has a method boolean accept(File directory, String filename) the classes which implement FilenameFilter should implement the accept method also.

The third method it returns the File objects as we specify. Here we can present a filter so that the File objects which passes the filter are accepted. FileFilter interface has a method boolean accept(File pathname) the classes which implements the interface should also implement the accept method.
ListFiles Alternattive
class FileFiltering
{
    public static void main(String arg[])
    {
        File f = new File("C:\\"); // LINE A
        FilenameFilter fnf = new Extension(".mp4");
        File[] farray = f.listFiles(fnf); // LINE B
        
        for(int i =0; i < farray.length; i++)
        {
            System.out.println(farray[i]);  // LINE C
        }
        System.out.println("------------------------");
        
        FileFilter ff = new CheckFilter();
        File ffarray[] = f.listFiles(ff); // LINE D
        
        for(int i =0; i < ffarray.length; i++)
        {
            System.out.println(ffarray[i]);
        }    
    }
}

class Extension implements FilenameFilter
{
    String name;
    public Extension(String name)
    {
        this.name = name;
    }
    public boolean accept(File dir, String name)
    {
        return name.endsWith(this.name);
    }
}
class CheckFilter implements FileFilter
{
    public boolean accept(File pathname)
    {
        return pathname.toString().endsWith("jpg");
    }
}
OUTPUT

C:\Java Beginner Tutorial 4.mp4
C:\Java Programming Tutorial 3.mp4
C:\Java Tutorial for beginners 2.mp4
C:\Java Tutorial for Beginners.mp4
------------------------
C:\20140626_123436.jpg
C:\20140626_123455.jpg
C:\20140626_123511.jpg
C:\20140626_123525.jpg

DESCRIPTION

In the above program we have two classes apart from main class. Extension implements FilenameFilter and implements accept method which filters files that end with the extension .mp4 and CheckFilter implements FileFilter and implements accept method which filters files that end with the extension .jpg
At LINE A we created a File Object and set the path to drive C:
At LINE B we are invoking the listFiles(FilenameFilter ffobj) method
At LINE C we are printing the File objects obtained from listFiles method.
At LINE D we are invoking listFiles(FileFilter fobj) method.

THINGS TO TRY
  • Obtain all text files from C: with listFiles(FilenameFilter ffobj)
  • Obtain all the directories in drive C: with listFiles(FileFilter fobj)

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.