map中
key 父路径
value 该路径下所有的文件
package com.sitech.ddoe.om.db.relational.test;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws JsonProcessingException {
Map map = new HashMap<>();
findFiles(map, "C:\\Users\\Administrator\\Desktop\\obj-conf-data");
System.out.println(new ObjectMapper().writeValueAsString(map));
}
public static void findFiles(Map map, String path) {
File file = new File(path);
File[] childFiles = file.listFiles();// 找出所有子目录
List fileList = new ArrayList<>();
for (int i = 0; childFiles != null && i < childFiles.length; i++) {
if (childFiles[i].isDirectory()) {// 如果是目录的话,则调用自身
findFiles(map, childFiles[i].getPath());
} else {
fileList.add(childFiles[i].getAbsolutePath());
String parPath = childFiles[i].getParent();
map.put(parPath, fileList);
}
}
}
}
最终的效果:
{
"C:\Users\Administrator\Desktop\obj-conf-data\crm\com\sitech\ddoe\client\api\objopt\sample": [
"C:\Users\Administrator\Desktop\obj-conf-data\crm\com\sitech\ddoe\client\api\objopt\sample\Student.xml",
"C:\Users\Administrator\Desktop\obj-conf-data\crm\com\sitech\ddoe\client\api\objopt\sample\User.xml"
],
"C:\Users\Administrator\Desktop\obj-conf-data\gg": [
"C:\Users\Administrator\Desktop\obj-conf-data\gg\ddd.txt",
"C:\Users\Administrator\Desktop\obj-conf-data\gg\hh.txt"
],
"C:\Users\Administrator\Desktop\obj-conf-data\order\com\sitech\ddoe\client\api\objopt\sample": [
"C:\Users\Administrator\Desktop\obj-conf-data\order\com\sitech\ddoe\client\api\objopt\sample\Student.xml",
"C:\Users\Administrator\Desktop\obj-conf-data\order\com\sitech\ddoe\client\api\objopt\sample\User.xml"
]
}