java以路径来生成目录,并添加数据到路径下

1.改进版,以dto对象来封装

 private static void addPath(ProjectTreeDTO root, String path, List<ProjectTablesDTO> tables) {
    StringBuilder url = new StringBuilder();

    String[] pathArr = path.split("/");

    for (String label : pathArr) {
        url.append("/").append(label);
        boolean flag = true;
        List<ProjectTreeDTO> children = root.getChildren();
        for (ProjectTreeDTO node : children) {
            if (node.getLabel().equals(label)) {
                root = node;
                flag = false;
                break;
            }
        }

        if (flag) {
            ProjectTreeDTO newNode = new ProjectTreeDTO(label,null, BdpConstant.DIRECTORY,null,BdpConstant.IMPORT_TABLE,root.getBackupId());
            List<ProjectTreeDTO> children2 = new ArrayList<>();

            String oldPath = label.substring(1);
            List<ProjectTablesDTO> levelTable = tables.stream().filter(u -> u.getImportPath().equals(oldPath)).collect(Collectors.toList());
            ProjectTreeDTO tableTree;
            for (ProjectTablesDTO table : levelTable) {
                tableTree = new ProjectTreeDTO(table.getTableName(), table.getTid(), BdpConstant.FILE, table.getTableType(), null);
                children2.add(tableTree);
            }

            newNode.setChildren(children2);
            children.add(newNode);
            root = newNode;
        }
    }

}

public static void createDirTree(ProjectTreeDTO importTableTree,List<ProjectTablesDTO> tables) {

    List<String> paths = tables.stream().map(ProjectTablesDTO::getImportPath).collect(Collectors.toList());

    List<ProjectTreeDTO> chirldren = new ArrayList<>();
    importTableTree.setChildren(chirldren);
    for (String path : paths) {
        addPath(importTableTree, path,tables);
    }
}

    public static void main(String[] args) throws Exception {
    ProjectTreeDTO parent = new ProjectTreeDTO("导入表",null, BdpConstant.DIRECTORY,null,BdpConstant.IMPORT_TABLE,666L);
    List<ProjectTablesDTO> tables = new ArrayList<>();
    //todo list.add(),list.add(),list.add()
    createDirTree(parent,tables);
    System.out.println(parent);
}

2.旧版,以map为来封装数据

 public class PathToTreeUtil2 {
    private static String getType(String filename, String path) {
        if (path.endsWith(filename)) {
            return "file";
        }
        return "folder";
    }

    private static void addPath(Map<String, Object> root, String path) {
        String url = "";
        String[] pathArr = path.split("/");
        for (String name : pathArr) {
            url += "/" + name;
            boolean flag = true;
            for (Map<String, Object> node : (List<Map<String, Object>>) root.get("content")) {
                if (node.get("name").equals(name)) {
                    root = node;
                    flag = false;
                    break;
                }
            }
            if (flag) {
                Map<String, Object> newNode = new HashMap<>();
                newNode.put("name", name);
                newNode.put("type", getType(name, path));
                newNode.put("url", url);
                newNode.put("content", new ArrayList<HashMap<String, Object>>());
                ((List<Map<String, Object>>) root.get("content")).add(newNode);
                root = newNode;
            }
        }
    }

    private static HashMap<String, Object> generate_data(String s) {
        String[] paths = s.split(",");
        HashMap<String, Object> root = new HashMap<>();
        root.put("name", "");
        root.put("url", "");
        root.put("type", "");
        ArrayList<String> arrayList = new ArrayList<>();
        root.put("content", arrayList);
        for (String path : paths) {
            addPath(root, path);
        }
        return root;
    }



    /****
     * 1. 主分析程序,拿到路径总的字符串,进行分析
     * @param path
     * @return
     */
    public static JSONObject pathToTree(String path) {
        Map<String, Object> root = new HashMap<>();
        root = generate_data(path);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", root);
        return jsonObject;
    }


    public static void main(String[] args) throws Exception {
        String str = "luo_t/ddr/liu,luo_t/luo/bb,cca/as21/df4";
        System.out.println(pathToTree(str).toString());

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值