我有一个要求,比如我需要通过遍历本地系统中的文件夹来形成一个josn,以便在树形结构中显示。(可能有任意数量的文件夹,子文件夹和文件)
我写了像
这样的代码 import java.io.File;
public class Sample {
public static void main(String[] args) {
File inputFolder = new File("D:\\gaja\\jQuery\\test");
traverse(inputFolder, "");
}
public static void traverse(File parentNode, String leftIndent) {
if (parentNode.isDirectory()) {
System.out.println(leftIndent + parentNode.getName());
// Use left padding to create tree structure in the console output.
leftIndent += " ";
File childNodes[] = parentNode.listFiles();
for (File childNode : childNodes) {
traverse(childNode, leftIndent);
}
} else {
System.out.println(leftIndent + parentNode.getName());
}
}
}
它正在成功遍历文件夹并按如下方式输出:
test
test1
resume.txt
test1.1
test1.1.1
test1.2
test2
test2.1
r.txt
test2.2
test3
hdhf.odt
但是我无法相应地形成json ..
我想要像这样的josn
[{ "folders": [
{ "name": "Folder 1", "files": [{ "name": "File 1.jpg"
}, { "name": "File 2.png" }], "folders": [
{ "name": "Subfolder 1", "files": [{ "name":
"Subfile 1.txt" }] },
{ "name": "Subfolder 2" }
]}
]},{ "folders": [
{ "name": "Folder 1", "files": [{ "name": "File 1.jpg" }, { "name": "File 2.png" }], "folders": [
{ "name": "Subfolder 1", "files": [{ "name": "Subfile 1.txt" }] },
{ "name": "Subfolder 2" }
]}
]},{ "folders": [
{ "name": "Folder 1", "files": [{ "name": "File 1.jpg" }, { "name": "File 2.png" }], "folders": [
{ "name": "Subfolder 1", "files": [{ "name": "Subfile 1.txt" }] },
{ "name": "Subfolder 2" }
]}
]},{ "folders": [
{ "name": "Folder 1", "files": [{ "name": "File 1.jpg" }, { "name": "File 2.png" }], "folders": [
{ "name": "Subfolder 1", "files": [{ "name": "Subfile 1.txt" }] },
{ "name": "Subfolder 2" }
]}
]}]
我希望我能得到解决方案。任何帮助将不胜感激。
答案 0 :(得分:0)
修改代码以返回元素而不是打印数据。以下是基于您当前代码并使用名为MyFile
的新类来存储文件中数据的示例:
public class MyFile {
private String name;
private List<MyFile> contents;
//constructor, getters and setters, other methods...
}
public class Sample {
public static MyFile traverse(File file) {
if (!file.exists()) return null;
MyFile myFile = new MyFile(file.getName());
if (parentNode.isDirectory()) {
File childNodes[] = parentNode.listFiles();
List<MyFile> contents = new ArrayList<>();
for (File childNode : childNodes) {
contents.add(traverse(childNode));
}
}
return myFile;
}
}
答案 1 :(得分:0)
有多种方法可以将字符串转换为JSON表示。
步骤1:在项目中包含JACKSON依赖项。 JACKSON库中有几个jar。 第2步:创建POJO。这些对象将转换为JSON和从JSON转换。 ... 第3步:将Java对象转换为JSON并将JSON写入文件。将CarFleet对象转换为JSON并将该JSON写入文件。
我建议你创建一个名为Folders的对象
class Folders {
List<Folders> folderList;
List<File> files;
String folderName;
}
class File{
String fileName;
}
通过创建上述结构并跟随http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/,您可以轻松转换为JSON对象。
答案 2 :(得分:0)
关于同样的事情:
private static void traverse(File parentNode, String leftIndent) {
System.out.println(leftIndent + "{");
String indent = leftIndent + " ";
System.out.print(indent + "\"name\":\""
+ parentNode.getName() + "\"");
if (!parentNode.isDirectory()) {
System.out.println();
} else {
array(parentNode.listFiles(file -> file.isFile()), "files", indent);
array(parentNode.listFiles(file -> file.isDirectory()), "folders", indent);
}
System.out.print(leftIndent + "}");
}
private static void array(File[] files, String attr, String leftIndent) {
if (files == null || files.length == 0) {
System.out.println();
} else {
System.out.println(",");
System.out.print(leftIndent + "\"" + attr + "\":[");
String indent = leftIndent + " ";
boolean first = true;
for (File file: files) {
if (first) {
System.out.println();
first = false;
} else {
System.out.println(",");
}
traverse(file, indent);
}
System.out.print("]");
}
}
更新:
现在让我们把它放在一个servlet中:
public class DirectoryServlet extends HttpServlet {
private static final File BASE_DIR = new File("D://");
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getParameter("path");
File file;
if (path == null || path.isEmpty()) {
file = BASE_DIR;
} else {
file = new File(BASE_DIR, path);
}
response.setContentType("application/json");
try (PrintWriter out = response.getWriter()) {
traverse(out, file, "");
}
}
private static void traverse(PrintWriter out, File parentNode,
String leftIndent) {
out.println(leftIndent + "{");
String indent = leftIndent + " ";
out.print(indent + "\"name\":\""
+ parentNode.getName() + "\"");
if (!parentNode.isDirectory()) {
out.println();
} else {
array(out, parentNode.listFiles(file -> file.isFile()), "files", indent);
array(out, parentNode.listFiles(file -> file.isDirectory()), "folders", indent);
}
out.print(leftIndent + "}");
}
private static void array(PrintWriter out, File[] files, String attr,
String leftIndent) {
if (files == null || files.length == 0) {
out.println();
} else {
out.println(",");
out.print(leftIndent + "\"" + attr + "\":[");
String indent = leftIndent + " ";
boolean first = true;
for (File file: files) {
if (first) {
out.println();
first = false;
} else {
out.println(",");
}
traverse(out, file, indent);
}
out.print("]");
}
}
@Override
public String getServletInfo() {
return "JSON directory structure";
}
}