File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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






Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[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 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Directory traversal problem