Skip to content
Related Articles

Related Articles

Improve Article

File listFiles() method in Java with Examples

  • Last Updated : 30 Jan, 2019
Geek Week

The listFiles() method is a part of File class.The function returns an array of Files denoting the files in a given abstract pathname if the path name is a directory else returns null. The function is an overloaded function. One of the function does not have any parameter, the second function takes FilenameFilter object as parameter, the third function takes FileFilter object as parameter

Function Signature:

public File[] listFiles()
public File[] listFiles(FilenameFilter f)
public File[] listFiles(FileFilter f)

Function Syntax:

file.listFiles()
file.listFiles(filter)

Parameters: The function is a overloaded function

  • One of the function does not have any parameter,
  • The second function takes FilenameFilter object as a parameter,
  • The third function takes FileFilter object as a parameter

Return value: The function returns a File array, or null value if the file object is a file.



Exception: This method throws Security Exception if the function is not allowed read access to the file

Below programs will illustrate the use of the listFiles() function

Example 1: We will try to find all the files and directories in a given directory




// Java program to demonstrate the
// use of listFiles() function
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try-catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("f:\\program");
  
            // Get all the names of the files present
            // in the given directory
            File[] files = f.listFiles();
  
            System.out.println("Files are:");
  
            // Display the names of the files
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

Output:

Files are:
1232.txt
1245.txt
5671.txt
program1

Example 2: We will try to find all the files and directories in a given directory whose names start with “12”




// Java program to demonstrate the
// use of listFiles() function
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try-catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("f:\\program");
  
            // Create a FilenameFilter
            FilenameFilter filter = new FilenameFilter() {
  
                public boolean accept(File f, String name)
                {
                    return name.startsWith("12");
                }
            };
  
            // Get all the names of the files present
            // in the given directory
            // and whose names start with "12"
            File[] files = f.listFiles(filter);
  
            System.out.println("Files are:");
  
            // Display the names of the files
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

Output:

Files are:
1232.txt
1245.txt

Example 3: We will try to find all the files and directories in a given directory which are text files




// Java program to demonstrate the
// use of listFiles() function
  
import java.io.*;
  
public class solution {
    public static void main(String args[])
    {
  
        // try-catch block to handle exceptions
        try {
  
            // Create a file object
            File f = new File("f:\\program");
  
            // Create a FileFilter
            FileFilter filter = new FileFilter() {
  
                public boolean accept(File f)
                {
                    return f.getName().endsWith("txt");
                }
            };
  
            // Get all the names of the files present
            // in the given directory
            // which are text files
            File[] files = f.listFiles(filter);
  
            System.out.println("Files are:");
  
            // Display the names of the files
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

Output:

Files are:
1232.txt
1245.txt
5671.txt

The programs might not run in an online IDE. please use an offline IDE and set the Parent file of the file

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :