Thursday, September 25, 2025
HomeLanguagesJavaFile list() method in Java with Examples

File list() method in Java with Examples

The list() method is a part of File class.The function returns an array of string 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 and the other function takes FilenameFilter object as parameter

Function Signature:

public String[] list()
public String[] list(FilenameFilter f)

Function Syntax:

file.list()
file.list(filter)

Parameters: The function is an overloaded function. One of the function does not have any parameter and the other function takes FilenameFilter object as a parameter

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

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

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

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




// Java program to demonstrate the
// use of list() 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
            String[] files = f.list();
  
            System.out.println("Files are:");
  
            // Display the names of the files
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i]);
            }
        }
        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 list() function
  
import java.io.*;
  
public class solution {
    public static void main(String atry-catch  {
  
        // 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"
            String[] files = f.list(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]);
            }
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }


Output:

Files are:
1232.txt
1245.txt

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

RELATED ARTICLES

Most Popular

Dominic
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6682 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6754 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS