Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Directory traversal problem

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am working on a problem that requires directory traversal .It reads directory names in simple text form like "project /abc/th" and creates a directory structure .

For that my initial development is like given below.ITs not complete yet.I think i need some another things to include for which i need help.



Eg my directories - Are project/src
project/etc

My Method Implementation :
myFolderService()- to return full FolderTree

Expected Output in my testcases :
topMap = ft.getChildren();// topmap size ->1


FolderTree tree1 = topMap.get("project");//foldername-project
projectmap= tree1.getChildren(); //map size =2


FolderTree src = ...get("src");
FolderTree event = …..("etc");
assertNotNull(src);
assertNotNull(event);

src.getChildren().size()--0);
event.getChildren().size()--- 0);


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?






 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having a very hard time understanding what you're trying to do, but I'm fairly sure you're making it much more complex than need be. Can't you just use the java.io.File.mkdirs() method?
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

rc
 
reply
    Bookmark Topic Watch Topic
  • New Topic