答案 0 :(得分:0)
尝试使用文件类中的方法列出文件:
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());
}
答案 1 :(得分:0)
不确定你想要如何展示它们,但这是一种方式:
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);
如果您正在构建GUI,我建议使用文件选择器: https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
答案 2 :(得分:0)
尝试使用ApacheIO。用最少的代码就可以实现它。
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).
工作示例: