I'd like to show in my Java application all files inside of a folder, like the windows explorer do.
I'd like to create a GUI like this:
There you can see, that all folders and files of a path are listed.
Does anyone has a good solution for that?
I'd like to show in my Java application all files inside of a folder, like the windows explorer do.
I'd like to create a GUI like this:
There you can see, that all folders and files of a path are listed.
Does anyone has a good solution for that?
Try listing the files usibng the method from the file class:
final File[] x = new File("C:\\").listFiles();
for (final File file : x) {
System.err.println(file.getName());
System.err.println(file.isDirectory());
System.err.println(file.isFile());
}
Not sure how exactly you want to show them but here is one way:
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
System.out.println(fileEntry.getName());
}
}
}
final File folder = new File("/home/you/Desktop");
listFilesForFolder(folder);
Props to: Read all files in a folder
In case you are building a GUI I would suggest using a File Chooser: https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
Try ApacheIO for this. With minimum code you can achieve it.
Class: FileUtils
Method:
iterateFiles(File directory, String[] extensions, boolean recursive)
Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.
iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
Allows iteration over the files in given directory (and optionally its subdirectories).
Working example: