Java递归实现遍历文件目录

2014-11-24 02:22:12 · 作者: · 浏览: 0

import java.io.File;
public class ListAllPath {
public void print(File mFile, int mlevel){
for(int i = 0; i < mlevel; i++){
System.out.print("\t");
}
if (mFile.isDirectory()){
System.out.println("<" + getPath(mFile) + ">");
String[] str = mFile.list();
for (int i = 0; i < str.length; i++){
print(new File(mFile.getPath() + "\\" + str[i]) , mlevel + 1);
}
}else{
System.out.println(getPath(mFile));
}
}

public String getPath(File mFile){
String fullPath = mFile.getPath();
String[] str = fullPath.split("\\\\");
return str[str.length - 1];
}

}

import java.io.File;

public class Demo {
public static void main(String[] args){
ListAllPath demoTest = new ListAllPath();
File rootFile = new File("E:\\job");
demoTest.print(rootFile, 0);
}
}