File list() method in Java with Examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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 // 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 Comment More infoAdvertise with us Next Article List size() method in Java with Examples A andrew1234 Follow Improve Article Tags : Java Java-Functions Java-IO package Java-File Class Practice Tags : Java Similar Reads List get() method in Java with Examples The get() method of List interface in Java is used to get the element present in this list at a given specific index. Example:Java// Java Program to demonstrate List // get() Method import java.util.*; class Main { public static void main (String[] args) { // Create a List List<Integer> a=new 2 min read File length() method in Java with Examples The length() function is a part of File class in Java . This function returns the length of the file denoted by the this abstract pathname was length.The function returns long value which represents the number of bytes else returns 0L if the file does not exists or if an exception occurs. Function s 2 min read List add() Method in Java with Examples The List add() method adds (appends) an element to a list. It can be used for both ArrayList and LinkedList. Example of List add() Method: Java // Java program to demonstrate // ArrayList usage import java.io.*; import java.util.ArrayList; import java.util.List; class GFG { public static void main ( 3 min read File lastModified() method in Java with Examples The lastModified() function is a part of File class in Java . This function returns the time denoted by the this abstract pathname was last modified.The function returns long value measured in milliseconds, representing the time the file was last modified else returns 0L if the file does not exists 2 min read List size() method in Java with Examples The size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container.Example:Java// Java Program to demonstrate // List size() Method import java.util.*; class GFG { public sta 2 min read Field get() method in Java with Examples The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an 3 min read Like