Home » Java programming language

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

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

File Class File[] listFiles()

  • This method is available in package java.io.File.listFiles().
  • This method is used to return the names of all the files in the form of an array of files which is represented in the filepath.
  • The return type of this method is File[] i.e. It returns an array of files 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 Second method accept one parameter(i.e FilenameFilter fnf) and Third method also accept one parameter (i.e. FileFilter).

Syntax:

    File[] listFiles(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is File[] i.e. it returns all files names in an array of files 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 ToListSpecificFiles {
    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
            FilenameFilter fnf = new FilenameFilter() {

                public boolean accept(File file, String name) {
                    return name.startsWith("J");
                }
            };
            // By using list(fnf) returns all the files and directories 
            // which is represented in a file path if file path is a 
            // directory and all file or directories starts with 'J'.

            String[] filelist = file.list(fnf);

            System.out.println("These are the name of files represented in a given directory :" + file.getPath());
            // 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]);

        } 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 :C:\Users\computer clinic\OneDrive\Articles
C:\Users\computer clinic\OneDrive\Articles\articles_list_june.docx
C:\Users\computer clinic\OneDrive\Articles\article_invoice_june.xlsx
C:\Users\computer clinic\OneDrive\Articles\DurgaSoft-SCJP-notes-Part-1_JavabynataraJ.pdf
C:\Users\computer clinic\OneDrive\Articles\DurgaSoft-SCJP-notes-Part-2_JavabynataraJ.pdf
C:\Users\computer clinic\OneDrive\Articles\How to add characters to a string in Java.docx



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.