
Program to find depth of the file in a directory and list all files those are having more number of parent 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);
}
}
}
For more information, please visit the following links:
http://www.roseindia.net/java/example/java/io/TraversingFileAndDirectories.shtml
http://www.roseindia.net/tutorial/java/core/files/filelistfiles.html
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.