数据结构存储目录结构? [英] Data structure to store a directory structure ?

查看:492
本文介绍了数据结构存储目录结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用struts 2框架开发一个简单的java web应用程序。应用程序的目的是使用JSP页面在我的电脑下显示特定的目录结构。



我的问题是要使用的数据结构为了存储目录结构,使JSP页面可以从action类访问该目录结构对象。



ps:我想使用以下java代码遍历目录。



Plz help

  import java.io.File; 

public class DisplayDirectoryAndFile {

public static void main(String args []){

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

public static void displayIt(File node){

System.out.println(node.getAbsoluteFile());

if(node.isDirectory()){
String [] subNote = node.list();
(String filename:subNote){
displayIt(new File(node,filename));
}
}

}
}


解决方案

目录结构非常容易被树木建模。您可以想到表示目录或文件的每个节点,边缘从目录运行到该目录的内容。



您可以通过使用节点类来表示树本身存储实体的名称(目录或文件),无论它是否是目录,以及从其子目录/文件的名称到这些子目录或文件的节点的映射。



希望这有帮助!


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

    }
}

解决方案

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!

这篇关于数据结构存储目录结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆