'Java Tree to represent filesystem (files/dir) from list of paths
I've a list of paths like this
/mnt/sdcard/folder1/a/b/file1
/mnt/sdcard/folder1/a/b/file2
/mnt/sdcard/folder1/a/b/file3
/mnt/sdcard/folder1/a/b/file4
/mnt/sdcard/folder1/a/b/file5
/mnt/sdcard/folder1/e/c/file6
/mnt/sdcard/folder2/d/file7
/mnt/sdcard/folder2/d/file8
/mnt/sdcard/file9
So from this list of paths (Stings) I need to crete a Java Tree structure that has folders as nodes and files as leaf (there wont be empty folders as leaf).
What I need I think is the add method where I pass to them a String (path of the file) and it add it to the correct place in the tree creating correct nodes (Folder) if they are not already there
This tree structure will need me to get list of nodes when I'm on node and list of leafs (but I think this will be a normal features for trees)
I will always have Strings as paths and not real file or folders. Is there something ready to use or a source code to start?
Thank you very much.
Solution 1:[1]
I implemented a solution to the challenge myself, it is available as a GitHubGist.
I am representing each node of a filesystem-hierarchy in a DirectoryNode. A helper-method createDirectoryTree(String[] filesystemList) creates a direcotry-tree.
Here is the usage example, which is included in the GitHubGist.
final String[] list = new String[]{
"/mnt/sdcard/folder1/a/b/file1.file",
"/mnt/sdcard/folder1/a/b/file2.file",
"/mnt/sdcard/folder1/a/b/file3.file",
"/mnt/sdcard/folder1/a/b/file4.file",
"/mnt/sdcard/folder1/a/b/file5.file",
"/mnt/sdcard/folder1/e/c/file6.file",
"/mnt/sdcard/folder2/d/file7.file",
"/mnt/sdcard/folder2/d/file8.file",
"/mnt/sdcard/file9.file"
};
final DirectoryNode directoryRootNode = createDirectoryTree(list);
System.out.println(directoryRootNode);
The System.out.println -output is:
{value='mnt', children=[{value='sdcard', children=[{value='folder1',
children=[{value='a', children=[{value='b', children=[{value='file1.file',
children=[]}, {value='file2.file', children=[]}, {value='file3.file',
children=[]}, {value='file4.file', children=[]}, {value='file5.file',
children=[]}]}]}, {value='e', children=[{value='c',
children=[{value='file6.file', children=[]}]}]}]},
{value='folder2', children=[{value='d', children=[{value='file7.file',
children=[]}, {value='file8.file', children=[]}]}]},
{value='file9.file', children=[]}]}]}
Solution 2:[2]
Seems like you could adapt either a Trie / Radix Trie or a Binary Search Tree to work in either situation. You could augment a Trie to store "folders" as the inner nodes (instead of characters like in a regular Trie) or you could augment a Binary Search Tree to store "folders" as inner nodes (as long as they implement a comparable interface) and "files" as leaf nodes.
My implementation of those structures are linked in the text above.
Solution 3:[3]
I would recommend reading up on data structures, particularly trees. In Java, you can represent these by creating a node class that has references to other nodes. For example:
public class Node {
private Node[] children;
/* snip other fields */
public boolean isFile() {
return children.count == 0;
}
}
Obviously, you can store your node references anyway you like- arrays or collections will work with non-binary trees.
Given your list of files, you can read these and populate your tree structure.
Solution 4:[4]
Have a look in new Java 7 - nio2 package. All you need is inside.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Markus Schulte |
| Solution 2 | |
| Solution 3 | David B |
| Solution 4 | cl-r |
