递归遍历d盘所有文件
package Io流;
import org.junit.Test;
import java.io.File;
public class Demo02 {
public static void showFile(String pathName){
File file = new File(pathName);
if (file.isDirectory()){
File[] files = file.listFiles();
if (files!=null){
for (File f:files) {
if (f.isDirectory()){
String path = f.getPath();
showFile(path);
}else {
String name = file.getName();
String path = file.getAbsolutePath();
System.out.println(name+"文件的路径是"+path);
}
}
}
}else {
String name = file.getName();
String path = file.getAbsolutePath();
System.out.println(name+"文件的路径是"+path);
}
}
/**
* 方法递归
* 1.判断文件是否是文件夹 isDirect();
* 2.listFiles()---->file[]
*/
public static void main(String[] args) {
String pathName = "E:\\";
showFile(pathName);
}
}
人生不易,多多努力!!!
浙公网安备 33010602011771号