public static void main(String[] args) {
showFile("E:\\");
}
public static void showFile(String pathname){
File file = new File(pathname);
//判断文件是否为文件夹
boolean flag = file.isDirectory();
if (flag){
File[] files = file.listFiles();
[图片上传中...(20220817151002.png-23f05a-1660720437356-0)]
//做非空判断,否则会出现空指针异常
for (int i = 0; files!=null && i < files.length; i++) {
showFile(files[i].getPath());
}
}else{
//获取此文件的路径
String filePath = file.getPath();
System.out.println("普通文件===================" + filePath);
}
}
运行效果
20220817151002.png