The list() method of File class is used to get the list of file names in the folder or directory.
Example:
package com.w3schools;
import java.io.File;
public class ListFileNames {
public static void main(String args[]){
File file = new File("D:/Test files/");
String[] fileList = file.list();
for(String name:fileList){
System.out.println(name);
}
}
} |
package com.w3schools;
import java.io.File;
public class ListFileNames {
public static void main(String args[]){
File file = new File("D:/Test files/");
String[] fileList = file.list();
for(String name:fileList){
System.out.println(name);
}
}
}
Output:
file1.docx
file2.docx
newTest.txt |
file1.docx
file2.docx
newTest.txt
Download this example.