Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file directory structure as JSON in Java?

What i am trying to accomplish is pretty straight forward. I want to create a method, which accept a root directory param as input, and return the file folder structure comes under input directory as JSON data. I started from the code below;

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

It prints directory and file list. I implemented an array list, and modified it as below;

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

It also does the same. What i am trying to accomplish is, to return the directory listing as JSON, something like an array, array inside array solution. Is this possible?

like image 988
Alfred Avatar asked Oct 17 '22 01:10

Alfred


1 Answers

If you would like to create a JSON it is better to work with Objects, so a possible solution can be:

This is a class representing a file.

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 + "]";
    }

}

This is a class representing a folder.

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 + "]";
}

And the main functionality:

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

}

For the JSON you need to include the jackson-mapper-asl artifact.

You should check it carefully before add to your program because i didn't test it much, but you can get the feeling.

like image 143
ddarellis Avatar answered Nov 01 '22 10:11

ddarellis