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 File.listFiles...doesn't return the files from the sub directory... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File.listFiles...doesn Watch "File.listFiles...doesn New topic
Author

File.listFiles...doesn't return the files from the sub directory...

kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
I did create a class that implements the FileFilter,
but it only returns the XML files from the current directory, not the sub directory...

What should I do if I want it to return the XML files from the subdirectory as well? here is my code


and here is my Test class that I test them out

Any ideas?

Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Some of the File objects in the current directory represent subdirectories. To list all the .xml files in this directory and all the subdirectories, you have to

1. Add all the XML files in the current directory to the list.
2. For each subdirectory, go to step 1.

It's most natural to implement this using recursion.


[Jess in Action][AskingGoodQuestions]
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
yeah..that is exactly what i did, only I didn't check your reply on time

Many thanks regardless
kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
but I think I'd also like to point out one thing though,

if the project directory you are working in has multiple sub folders, wouldn't you potentially run into a stack overflow exception?


i guess this could still work for a normal project directory that has no more than 3 sub levels...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes, recursion always runs the risk of stack overflow. It's possible to implement this using an iterative method and an explicit stack (this is true of any recursive algorithm.) Push the initial directory onto a java.util.Stack. Then

 
wood burning stoves
 
subject: File.listFiles...doesn't return the files from the sub directory...