Primefaces树节点文件浏览器



我正在尝试创建一个素数面树节点,该节点显示包含一个目录的文件或目录的名称。这就是我现在得到的。

问题是代码将节点保存到上一个节点中,但我想要的是,如果我们传递到列表中的下一个值,它会检查p。例如,如果节点C:/存在,如果它存在,它会检测其中的下一节点是否存在,如果存在,它也会递归地执行同样的操作。

public void init() {
root = new DefaultTreeNode("Files", null);
try (Stream<Path> paths = Files
.walk(Paths.get("C:\Path"))) {
List<String> list = paths.map(path -> Files.isDirectory(path) ? path.toString() + '/' : path.toString())
.collect(Collectors.toList());
for (int i = 0; i < list.size(); i++) {
String[] prueba = list.get(i).split("\\");
List<String> listaprueba = new ArrayList<String>(Arrays.asList(prueba));
//removes C:/
listaprueba.remove(0);
prueba = listaprueba.toArray(new String[0]);
for (int e = 0; e < prueba.length; e++) {
if (root.getChildCount() == 0) {
setNode0(new DefaultTreeNode(prueba[0], root));
} else {
if (prueba[e].equals(prueba[prueba.length - 1])) {
node0.getChildren().add(new DefaultTreeNode(prueba[e], node0));
} else {
setNode0(new DefaultTreeNode(prueba[e], node0));
}
}
}
}
}
catch (
IOException e) {
System.out.println("Archivo no encontrado.");
}
}

刚刚修复了它,比我更容易。

private class Tree{
private TreeNode root;
private void process(File mainDir, TreeNode root    ) {
if (mainDir.isDirectory()) {
TreeNode dad= new DefaultTreeNode(mainDir.getName(), root);
for (File son: mainDir.listFiles()) {
process(son, dad);
}
} else {
root.getChildren().add(new DefaultTreeNode(mainDir.getName()));
}
}   
@PostConstruct
public void init() {
File mainDir = new File("InsertPathHere");
root = new DefaultTreeNode("", null);
process(mainDir, root);
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
}

最新更新