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