我试图用来FileUtils.listFilesAndDirs
显示给定路径中的所有文件和文件夹,但目前我的代码只获取文件并过滤掉给定路径中的目录。
//Ask for the path to the directory
System.out.println("Enter path: ");
path = scanner.nextLine();
File myFile = new File(path);
if(myFile == null || !myFile.exists()) {
System.out.println("Bad directory path!");
System.exit(-1);
}
//Make a filter that matches files and directories
final IOFileFilter myFilter = new IOFileFilter() {
@Override
public boolean accept(File dir, String name) {
return true;
}
@Override
public boolean accept(File file) {
return true;
}
};
//List files and folders in that directory
Collection<File> listOfFiles = FileUtils.listFilesAndDirs(myFile, myFilter, null);
for(File file : listOfFiles) {
if(file.isDirectory()) {
System.out.print("Directory: ");
}
else {
System.out.println("File: ");
}
System.out.print(file.getAbsolutePath());
}