如何在Java中将文件目录结构作为JSON?

时间:2017-12-19 09:35:48

标签: java arrays json data-structures directory

我想要完成的事情非常简单。我想创建一个方法,它接受一个根目录param作为输入,并返回文件夹结构作为JSON数据在输入目录下。我从下面的代码开始;

public class dirscan {
    public static void main (String args[]) {
        displayIt(new File("D:\MyDir"));
    }

    public static void displayIt(File node) {
        System.out.println(node.getAbsoluteFile());
        if (node.isDirectory()) {
            String[] subNote = node.list();
            for (String filename : subNote) {
                displayIt(new File(node, filename));
            }
        }
    }
}

打印目录和文件列表。我实现了一个数组列表,并将其修改如下;

public class DirScan {
    static List<String> allList = new ArrayList<String>();

    public static void main(String args[]) {
        List<String> mylist = displayIt(new File("D:\Books"));
        for (String filename : mylist) {
            System.out.println(filename);
        }
    }

    public static List<String> displayIt(File node) {
        allList.add(node.getAbsoluteFile().toString());
        if (node.isDirectory()) {
            String[] subNote = node.list();
            for (String filename : subNote) {
                displayIt(new File(node, filename));
            }
        }
        return allList;
    }
}

它也是如此。我想要完成的是,将目录列表作为JSON返回,类似于数组,数组解决方案中的数组。这可能吗?

1 个答案:

答案 0 :(得分:2)

如果您想创建JSON,最好使用Objects,因此可能的解决方案是:

这是一个代表文件的类。

public class CustomFile {

    private String name;

    public CustomFile(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "CustomFile [name=" + name + "]";
    }

}

这是一个代表文件夹的类。

public class CustomFolder {

    private String name;

    List<CustomFile> files;

    List<CustomFolder> folders;

    public CustomFolder(String name) {
        files = new ArrayList<>();
        folders = new ArrayList<>();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<CustomFile> getFiles() {
        return files;
    }

    public void addFile(CustomFile file) {
        this.files.add(file);
    }

    public List<CustomFolder> getFolders() {
        return folders;
    }

    public void addFolder(CustomFolder folder) {
        this.folders.add(folder);
    }

    @Override
    public String toString() {
        return "CustomFolder [name=" + name + ", files=" + files + ", folders=" + folders + "]";
}

主要功能:

public class Main {
    static List<String> allList = new ArrayList<String>();

    public static void main(String[] args) throws IOException {
        CustomFolder parentFolder = new CustomFolder("path/to/folder");
        CustomFolder mylist = displayIt(parentFolder);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(mylist));
    }

    public static CustomFolder displayIt(CustomFolder parentFolder) throws IOException {
        File node = new File(parentFolder.getName());
        if (node.isDirectory()) {
            String[] subNote = node.list();
            for (String filename : subNote) {
                String path = node + "\\" + filename;
                if (new File(path).isDirectory()) {
                    CustomFolder folder = new CustomFolder(path);
                    parentFolder.addFolder(folder);
                    displayIt(folder);
                } else {
                    parentFolder.addFile(new CustomFile(path));
                }
            }
        }
        return parentFolder;
    }

}

对于JSON,您需要包含jackson-mapper-asl工件。

在添加到您的程序之前,您应该仔细检查,因为我没有测试它,但你可以感受到。