The moose likes Java in General and the fly likes Directory traversal problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Directory traversal problem " Watch "Directory traversal problem " New topic

Directory traversal problem

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278

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

Joined: Jul 08, 2003
Posts: 24199

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 method?

[Jess in Action][AskingGoodQuestions]
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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 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 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.

I agree. Here's the link:
subject: Directory traversal problem
jQuery in Action, 3rd edition