• Post Reply Bookmark Topic Watch Topic
  • New Topic

Directory traversal problem

Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • 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

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");

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?

Ernest Friedman-Hill
author and iconoclast
Posts: 24213
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • 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.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!