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
public String list() public String list(FilenameFilter f)
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
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”
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
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.