user2910459
user2910459

Reputation: 7

Create Folders recursively

I call a webservice and get the following data from it:

    Name of the folder
    Id of the folder
    Id of the parent-folder (null if it is root)

I create ArrayLists (List<String>) for the names, the ids and the parent-ids. So the folder with the name on position "0" has the id and the parent-id on position "0" in these lists.

Now I need to recreate the same structure on my local file system. The user enters a root-directory ("C:\test" for example) that I need to use.

I guess that a recursive method would be the best thing to do, but I have no idea how to implement it.

Any ideas / hints?

Upvotes: 0

Views: 127

Answers (1)

arcy
arcy

Reputation: 13153

I don't see how recursion helps you. I assume you get multiple sets of the data you present, implied by your explanation though you don't say so. You also don't say what order you get them in. I'd create a hashmap, using full path to each parent as a key, and an object representing the directory as a value. The directory object would contain pointers to all its child directories. I'd create that entire hashmap, then walk it top-down. If you don't get the data in the correct order to build it top-down, then you'll have to put them all in a list and search the list to create top-down order, or trust that you can build the list without the IDs and fill them in later

Upvotes: 1

Related Questions