Java 遍历文档
- 摘要:importjava.io.File;importjava.util.ArrayList;importjava.util.List;publicclassGetFileTree{/***@paramargs*/publicstaticvoidmain(String[]args){GetFileTreefileTree=newGetFileTree();Listlist=fileTree.getTree("D:/cCode");//for(inti=0;i<list.size();i++)
- 标签:文档 遍历 Java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class GetFileTree {
/**
* @param args
*/
public static void main(String[] args) {
GetFileTree fileTree = new GetFileTree();
List list = fileTree.getTree("D:/cCode");//
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public List getTree(String path){
File file = new File(path);
List list = new ArrayList();
if(file.isDirectory()){
File[] filelist = file.listFiles();
for (File f : filelist) {
if(f.isDirectory()){
//System.out.println(f.getName());
getTree(path+"/"+f.getName());
}else{
list.add(f.getName());
System.out.println(f.getName());
}
}
}
return list;
}
}