Java根据文件相对路径生成文件树的结构

该博客介绍如何在Java中根据文件的相对路径创建文件树的结构,并展示了转义后的字符串结果以及包含文件ID的文件树JSON数据。
摘要由CSDN通过智能技术生成
 package cn.neuq.hcg.everyday;

import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/****
 * 根据字符串文件相对路径生成文件树结构
 */
public class PathToTreeUtil {
   
    public static String getType(String filename, String path) {
   
        if (path.endsWith(filename)) {
   
            return "file";
        }
        return "folder";
    }

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

    public 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;
    }

    public static String readFile(String pathname) throws Exception {
   
        String str = "";
        File file = new File(pathname);
        try {
   
            FileInputStream in = new FileInputStream(file);
            // size  为字串的长度 ,这里一次性读完
            int size = in.available();
            byte[] buffer = new byte[size];
            in.read(buffer);
            in.close();

            str = new String(buffer, "GB2312");
        } catch (IOException e) {
   
            // TODO Auto-generated catch block
            return null;

        }

        return str;


    }


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

    public static String zhuanYi(String path) {
   
        System.out.println(path);
        //将双引号去掉,将多余的空字符去掉
        path = path.replaceAll("\"", "").replaceAll(" ", "");

        /**
         * 把\\转成/
         */
        if (path.contains("\\\\")) {
   
            path = path.replaceAll("\\\\", "/");
            // path =c/a/bb//cc/dd//ee
            System.out.println("把\\\\转成/");
            System.out.println(path);
        }

        if (path.contains("//")) {
   
            path = path.replaceAll("//", "/");
            System.out.println("把//变成/");
            System.out.println(path)
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值