Skip to content
Advertisement

Java Tree to represent filesystem (files/dir) from list of paths

I’ve a list of paths like this

JavaScript

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.

Advertisement

Answer

Thank you for all your answer. I made my working implementation. I think that I will need to improve it in order to make it works better with more caching in adding element to the tree structure.

As I said what I was needing was a structure that allow me to have a “virtual” rappresentation of a FS.

MXMTree.java

JavaScript

MXMNode.java

JavaScript

Test.java for test code

JavaScript

Please tell me if you have some good advice about improvments 🙂

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement