java将本地文件目录转成树结构(递归)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件目录转成树结构
 * @author lph
 */
public class FileTreeUtil {
    /**
     * 用来存放数据 可存库
     */
    private static List<Tree> list = new ArrayList<>();
    /**
     * 因为测试使用,当初主键id来用
     */
    private static Integer id = 0;

    public static void main(String[] args) {

        //扫描此文件夹下面的所有文件
        String filepath = "e://test";
        //初始化父节点id
        int parentid = 0;
        try {
            file(filepath, parentid);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < list.size(); i++) {
            Tree tree = list.get(i);
            System.out.println("id==  " + tree.getId() + "  parentId==  " + tree.getParentId() + "  url==  " + tree.getPath());
        }
    }

    public static void file(String filepath, int parentid) throws FileNotFoundException {
        File file = new File(filepath);
        //1.判断文件
        if (!file.exists()) {
            throw new FileNotFoundException("文件不存在");
        }
        //2.文件
        if (file.isFile()) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            Tree tree = new Tree(id++, name, path, parentid);
            list.add(tree);
            return;
        }
        //3.获取文件夹路径下面的所有文件递归调用;
        if (file.isDirectory()) {
            String name = file.getName();
            String path = file.getAbsolutePath();
            Tree tree = new Tree(id++, name, path, parentid);
            list.add(tree);
            String[] list = file.list();
            for (int i = 0; i < list.length; i++) {
                String s = list[i];
                //根据当前文件夹,拼接其下文文件形成新的路径
                String newFilePath = path + "\\" + s;
                file(newFilePath, tree.getId());
            }
        }
    }
}

class Tree {
    private Integer id;
    //文件夹或者文件名称
    private String name;
    //全路径,或则部分路径,自己决定
    private String path;
    //父节点id
    private Integer parentId;

    public Tree() {
    }

    public Tree(Integer id, String name, String path, Integer parentId) {
        this.id = id;
        this.name = name;
        this.path = path;
        this.parentId = parentId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    @Override
    public String toString() {
        return "Tree{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", path='" + path + '\'' +
                ", parentId=" + parentId +
                '}';
    }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
将查询结果转换成的具体实现取决于查询结果的结构的定义方式。但是,一般来说,您可以使用以下步骤将查询结果转换成: 1. 创建一个节点类,用于表示的节点; 2. 遍历查询结果,将每一行数据转换成一个节点,并将节点按照的定义方式连接起来; 3. 返回根节点,即整个的入口。 下面是一个示例代码片段,它演示了如何将查询结果转换成: ```java public class Node { private int id; private String name; private List<Node> children; // 省略构造函数和其他方法 } public Node createTreeFromQueryResult(List<Map<String, Object>> queryResult) { Map<Integer, Node> nodeMap = new HashMap<>(); Node root = null; for (Map<String, Object> row : queryResult) { int id = (int) row.get("id"); String name = (String) row.get("name"); int parentId = (int) row.get("parent_id"); Node node; if (!nodeMap.containsKey(id)) { node = new Node(id, name, new ArrayList<>()); nodeMap.put(id, node); } else { node = nodeMap.get(id); node.setName(name); } if (parentId == 0) { root = node; } else { Node parent; if (!nodeMap.containsKey(parentId)) { parent = new Node(parentId, null, new ArrayList<>()); nodeMap.put(parentId, parent); } else { parent = nodeMap.get(parentId); } parent.getChildren().add(node); } } return root; } ``` 这个示例假设查询结果是一个包含每个节点的 ID、名称和父节点 ID 的列表。该函数遍历查询结果并创建节点对象。它使用一个哈希表来存储每个节点,以便在连接节点时可以快速检索。最后,该函数返回根节点,即整棵的入口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序三两行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值