结果
上面的代码示例将产生以下结果。
sdk
---vehicles
------body.txt
------color.txt
------engine.txt
---ships
------shipengine.txt
以下是在 Java 中搜索目录中所有文件的另一个示例示例
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("Enter the path to folder to search for files");
Scanner s1 = new Scanner(System.in);
String folderPath = s1.next();
File folder = new File(folderPath);
if (folder.isDirectory()) {
File[] listOfFiles = folder.listFiles();
if (listOfFiles.length < 1)System.out.println(
"There is no File inside Folder");
else System.out.println("List of Files & Folder");
for (File file : listOfFiles) {
if(!file.isDirectory())System.out.println(
file.getCanonicalPath().toString());
}
}
else System.out .println("There is no Folder @ given path :" + folderPath);
}
}
上面的代码示例将产生以下结果。
Enter the path to folder to search for files
C:/
List of Files & Folder
C:\bootmgr
C:\BOOTNXT
C:\hiberfil.sys
C:\pagefile.sys
C:\recovery.img.img
C:\swapfile.sys