Data structure to store a directory structure ?

[亡魂溺海] 提交于 2019-12-23 10:17:35

问题


I am developing a simple java web application using struts 2 framework. the purpose of the application is to display a specific directory structure under my computer using a JSP page.

My question is which data structure to use in order to store a directory structure, so that the JSP page can access that directory structure object from the action class.

ps:I want to use the following java code to traverse the directory.

Plz help

import java.io.File;

public class DisplayDirectoryAndFile{

    public static void main (String args[]) {

        displayIt(new File("C:\\Downloads"));
    }

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

    }
}

回答1:


Directory structures are very easily modeled by trees. You can think of each node representing a directory or file, with edges running from directories to the contents of that directory.

You could represent the tree itself by having a node class that stores the name of the entity (directory or file), whether or not it is a directory, and a map from the names of its subdirectories / files to the nodes for those subdirectories or files.

Hope this helps!




回答2:


As said, you can (and should) use a Tree.

This SO answer gives you a nice Tree Java structure out of the box: https://stackoverflow.com/a/3522481/1654265

read the comments too.



来源:https://stackoverflow.com/questions/13755495/data-structure-to-store-a-directory-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!