Data structure to store directory structure?

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

My question is which data structure to use to maintain the directory structure so that the JSP page can access this directory structure object from the action class.

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

Help Plz

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)); } } } } 
+4
source share
2 answers

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

You can represent the tree directly using the node class, which stores the name of the entity (directory or file), regardless of whether it is a directory, and a map from the names of its subdirectories / files to nodes for these subdirectories or files.

Hope this helps!

+3
source

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

This SO answer gives you a nice Java Tree structure out of the box: fooobar.com/questions/16983 / ...

also read the comments.

+1
source

All Articles