|
How to follow a directory structure |
|
|
You can use the following code to implement your own file browser in Java. visitAllDirsAndFiles method traverses all directories and files in a given directory. visitAllDirsAndFiles method does the same only for directories. process() method should be defined by you based on your needs..
// Process all files and directories under dir
public static void visitAllDirsAndFiles(File dir) {
// You can do whatever you want with this directory
// E.g. printing its name to the console
process(dir);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllDirsAndFiles(new File(dir, children[i]));
}
}
}
// Process only directories under dir
public static void visitAllDirs(File dir) {
if (dir.isDirectory()) {
// You can do whatever you want with this directory
// E.g. printing its name to the console
process(dir);
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
visitAllDirs(new File(dir, children[i]));
}
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.