使用java获取一个文件夹路径下的所有文件(递归查找),并封装为json

该代码实现了一个将文件系统目录结构转换为JSON格式的功能。它遍历指定路径的文件和子目录,为每个文件或目录生成包含名称、路径、是否为目录等信息的JSON对象。对于空目录,会添加一个表示“暂无文件”的条目。
摘要由CSDN通过智能技术生成
StringBuilder buf=new StringBuilder(); 

public String fileToJsonByPath(String path) {
        if(path.isEmpty()){
            return null;
        }
        File f = new File(path);
        if (f.isDirectory())
        {
            scanToJson(new File(path));
            buf.delete(buf.length() - 2, buf.length());
        }
        else {
            System.out.format("{\"name\" : \"%s\"}", f.getName());
        }
        return buf.append("}").toString();

    }



public void scanToJson(File f) {
        if (f.isDirectory())
        {
            buf.append("{").append("\"name\" : \"").append(f.getName()).append("\",").append("\"filePath\":\"\",").append("\"isDir\":\"0\",").append("\"children\":[");
            Arrays.asList(f.listFiles()).forEach(this::scanToJson);
            if(buf.toString().endsWith("\"children\":[")) {
                buf.append("{\"name\":\"暂无文件\",\"path\":\"\",\"isDir\":\"0\"},");
            }
            buf.delete(buf.length() - 2, buf.length());
            buf.append("}").append("]").append("},");
        }
        else {
            buf.append("{").append("\"name\" : \"").append(f.getName()).append("\"").append(",\"filePath\":").append(JSON.toJSONString(f.getAbsolutePath())).append(",\"isDir\":\"1\"").append("},");
        }
    }

自己再把string转成json传给前端就行

返回的json串

{
    "children": [
        {
            "children": [
                {
                    "children": [
                        {
                            "filePath": "D:\\Download\\202201\\excel\\20220101——excel.txt",
                            "name": "20220101——excel.txt",
                            "isDir": "1"
                        }
                    ],
                    "filePath": "",
                    "name": "excel",
                    "isDir": "0"
                },
                {
                    "children": [
                        {
                            "filePath": "D:\\Download\\202201\\pdf\\新建 Microsoft Excel 工作表.xlsx",
                            "name": "新建 Microsoft Excel 工作表.xlsx",
                            "isDir": "1"
                        }
                    ],
                    "filePath": "",
                    "name": "pdf",
                    "isDir": "0"
                }
            ],
            "filePath": "",
            "name": "202201",
            "isDir": "0"
        },
        {
            "children": [
                {
                    "path": "",
                    "name": "暂无文件",
                    "isDir": "0"
                }
            ],
            "filePath": "",
            "name": "202202",
            "isDir": "0"
        },
        {
            "children": [
                {
                    "filePath": "D:\\Download\\aaa\\新建 文本文档.txt",
                    "name": "新建 文本文档.txt",
                    "isDir": "1"
                }
            ],
            "filePath": "",
            "name": "aaa",
            "isDir": "0"
        },
        {
            "filePath": "D:\\Download\\ca.cer",
            "name": "ca.cer",
            "isDir": "1"
        },
        {
            "children": [
                {
                    "children": [
                        {
                            "path": "",
                            "name": "暂无文件",
                            "isDir": "0"
                        }
                    ],
                    "filePath": "",
                    "name": "houhuiyao",
                    "isDir": "0"
                }
            ],
            "filePath": "",
            "name": "DwnlData",
            "isDir": "0"
        },
        {
            "filePath": "D:\\Download\\exportAll1670495117699.xlsx",
            "name": "exportAll1670495117699.xlsx",
            "isDir": "1"
        },
        {
            "filePath": "D:\\Download\\exportAll1670495147848.xlsx",
            "name": "exportAll1670495147848.xlsx",
            "isDir": "1"
        },
        {
            "filePath": "D:\\Download\\exportAll1670495173838.xlsx",
            "name": "exportAll1670495173838.xlsx",
            "isDir": "1"
        },
        {
            "filePath": "D:\\Download\\lanserverproperties-1.9.1-forge.jar",
            "name": "lanserverproperties-1.9.1-forge.jar",
            "isDir": "1"
        },
        {
            "filePath": "D:\\Download\\ServerPropertiesLAN-2.65-1.7.10.jar",
            "name": "ServerPropertiesLAN-2.65-1.7.10.jar",
            "isDir": "1"
        },
        {
            "filePath": "D:\\Download\\stdout",
            "name": "stdout",
            "isDir": "1"
        },
        {
            "filePath": "D:\\Download\\stdout (1)",
            "name": "stdout (1)",
            "isDir": "1"
        }
    ],
    "filePath": "",
    "name": "Download",
    "isDir": "0"
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值