In order to list the contents of a directory, below program can be used. π
This program simply receives the names of the all sub-directory and files in a folder in an Array and then that array is sequentially traversed to list all the contents. π
import java.io.*;
public class ListContents {
public static void main(String[] args) {
File file =
new File("//home//user//Documents/");
String[] files = file.list();
System.out.println (
"Listing contents of " + file.getPath()
);
for(int i=0 ; i < files.length ; i++) {
System.out.println(files[i]);
}
}
}
ββββββββββββ
#java #io
@ProgrammingTip