
How to find all files in our local computer using java?

Hi Friend,
We are providing you the code that will display all the files of given directory and its sub directories.
import java.io.*;
import java.util.*;
class ListAllFiles{
static int size = 20;
public static void main(String[] args) {
System.out.print("Enter the name of a directory for listing:");
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String root = in.next();
File file = new File(root);
if (file != null && file.isDirectory()) {
displayList(file, 0);
}
}
}
public static void displayList(File f, int depth) {
if(f.isFile()){
System.out.println(f.getName());
}
if (f.isDirectory() && depth <= size) {
File[] dir = f.listFiles();
for (File ff : dir) {
displayList(ff, depth+1);
}
}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.