This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
In topNode- Project -It Maps-should have 1 only entry
name should be "project" and Directory tree for "project" node
I tried using recursion but falling into indefinite flow.
I was able to implement it this way-
Project -Tree's Map would have 2 entries in map (2 children,src,etc ) but thats is no we want.
Requesting each node should be able to retrun dir-tree for that.
Please suggest a way of implementing this behaviour and does it include recursion?
I am not sure what you are asking either, but my own interpretation is that you want to take the name of a directory and return a data structure representing that entire directory subtree - is that right? The mkdirs method creates a directory, but I think all you want to do is list directories.
I would (and did) certainly use recursion for the purpose.
General algorithm: starting from a string, create a java.io.File object for that directory. If that fails, you don't have a directory by that name, and I guess you are done.
If you do have a file, then you can use java.io.File.list() or .listFiles() to get the files and subdirectories in your top directory. I would use listFiles, giving you a File object for each one.
Using that information, I created a method that would take a File object and return an instance of an object containing the directory subtree starting from that object (which needs to be a directory as opposed to a file).
Within that method is a call to listFiles; for each file, if it is a file it does something intelligent, and if it is a directory it calls itself, passing in that directory File object and getting back an instance of whatever object I want to represent a subdirectory.
Whatever object that method returns, it should be suitable for putting in a collection of some sort to represent a directory in your data structure.
Hope that's enough to get you going. One of the fun parts of recursion is that, once you are done, it is hard to believe that so small an amount of code can do so much.