Directories
Start at the root; list all files:
import java.nio.filesystems.*;
import java.io.*;
public class FileSystemLister {
public static void main(String[] args) throws IOException {
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
FileReference ref = roots[i].toFileReference();
Directory root = ref.openDirectory();
list(root);
}
}
public static void list(Directory dir) {
try {
for (Directory.Entry entry : dir) {
FileReference ref = entry.asFileReference();
System.out.println(entry.getName());
try {
Directory child = ref.openDirectory();
// aliases?
list(child);
}
catch (NotDirectoryException ex) {
// There must be a better way to tell
// if a file is a directory before we open it
}
}
}
finally {
dir.close();
}
}
}