求助,Java递归遍历文件夹显示为树形菜单Jtree 20
2013-04-01
展开全部
把文件夹加载到Tree,输出全部文件夹中的文件全限定地址,代码如下
import java.util.List;
import java.util.Map;
public class TreeVo {
private List<TreeVo> children;
private String filename;
public TreeVo(String filename){
this.filename=filename;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public List<TreeVo> getChildren() {
return children;
}
public void setChildren(List<TreeVo> children) {
this.children = children;
}
public void addchild(TreeVo vo){
children.add(vo);
}
public boolean islast(){
return children==null;
}
}
package com.tomcat.one;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.tomcat.one.vo.TreeVo;
public class FileToTree {
public static void fileToTree(TreeVo root){
File rootfile=new File(root.getFilename());
if(!rootfile.isDirectory()){
return;
}else{
File[] children=rootfile.listFiles();
for(File child:children){
TreeVo childtree=new TreeVo(root.getFilename()+"\\"+child.getName());
if(root.getChildren()==null){
root.setChildren(new ArrayList<TreeVo>());
}
root.addchild(childtree);
fileToTree(childtree);
}
}
}
public static void fillstrlist(TreeVo vo, List strlist, String str) {
if (vo.islast()) {
strlist.add(vo.getFilename());
} else {
List<TreeVo> list = vo.getChildren();
for (TreeVo base : list) {
fillstrlist(base, strlist, base.getFilename());
}
}
}
public static void main(String[] args) {
TreeVo root=new TreeVo("E:\\java");
fileToTree(root);
List<String> strlist=new ArrayList<String>();
fillstrlist(root,strlist,"");
for(String fileFullName:strlist){
System.out.println(fileFullName);
}
}
}
不懂的可以问我,长期招收JAVA学员,三月保证学会,学完可享受终生指导。有意者联系Q:八七一三二三二九
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询