Home » Java programming language

Java File Class File[] listFiles(FileFilter ff) method with Example

Java File Class File[] listFiles(FileFilter ff) method: Here, we are going to learn about the File[] listFiles(FileFilter ff) method of File class with its syntax and example.
Submitted by Preeti Jain, on July 10, 2019

File Class File[] listFiles(FileFilter ff)

  • This method is available in package java.io.File.listFiles(FileFilter fnf).
  • This method is used to return an array of filepath indicates files and directories in a given directory which is represented in the filepath that satisfy specified filter.
  • The return type of this method is File[] i.e. It returns an array of filepath indicates files or directories which is represented in filepath if the given path is directory else return null.
  • This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.
  • This method is overridable. The first method does not accept any parameter and the second method accept one parameter and Third method is also accept one parameter.

Syntax:

    File[] listFiles(FileFilter ff){
    }

Parameter(s):

We pass only one object FileFilter as a parameter in the method and with the help of this argument, we can find files or directories with filter(i.e we can find specific files).

Return value:

The return type of this method is File[] i.e. it returns an array of filepath indicates all file names or directories which is represented in a filepath.

Java program to demonstrate example of listFiles() method

// import the File class because we will use File class methods
import java.io.*;

// import the Exception class because it may raise an 
// exception when working with files
import java.lang.Exception;

public class ToListFiles {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");

            // Create a FilenameFilter object
            FileFilter ff = new FileFilter() {

                public boolean accept(File file) {
                    return file.getName().endsWith("pdf");
                }
            };
            // By using list(ff) returns an array of file indicates all 
            // the files and directories which is represented in a file path 
            // if file path is a directory and all file or 
            // directories ends with 'pdf'.
            File[] filelist = file.listFiles(ff);

            System.out.println("These are the name of files represented in a given directory whose name ends with doc :");
            // By using loop to traverse the filenames and directories 
            // in the given path .
            for (int i = 0; i < filelist.length; i++)
                System.out.println(filelist[i].getName());
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac ToListFiles.java

D:\Programs>java ToListFiles
These are the name of files represented in a given directory whose name ends with pdf :
DurgaSoft-Part-1_JavabynataraJ.pdf
DurgaSoft-Part-2_JavabynataraJ.pdf





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.