【实现方法1】
代码A
import java.io.File;
public class TraverseFile {
public void traverseFile(String path) {
File file = new File(path);
if (file.exists()) {
File[] fileArray = file.listFiles();
for (File f : fileArray) {
if (f.isDirectory()) {
System.out.println("【文件夹】-----" + f.getAbsolutePath());
traverseFile(f.getAbsolutePath());
} else {
System.out.println("【文件】-----" + f.getAbsolutePath());
}
}
} else {
System.out.println("文件不存在!");
}
}
public static void main(String[] args) {
TraverseFile traverseFile = new TraverseFile();
traverseFile.traverseFile("D:/ME-APP");
}
}
代码B
import java.io.File;
public class TraverseFile {
public void traverseFile(File sourceDir) {
if (sourceDir.isFile()) {
System.out.println(file.getAbsolutePath());
} else {
File[] fs = sourceDir.listFiles();
if (fs != null && fs.length > 0) {
for (File ff : fs) {
if (ff.isFile()) {
System.out.println(ff.getAbsolutePath());
} else {
traverseFile(ff);
}
}
}
}
}
public static void main(String[] args) {
TraverseFile traverseFile = new TraverseFile();
File file = new File("D:\\ME-APP");
traverseFile.traverseFile(file);
}
}