This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File.listFiles...doesn't return the files from the sub directory...
 
Similar Threads
File Class question
Listing directory contents as per filter
Java BufferedReader and BufferedWriter
Internal Class
Is it possible to write a reg exp to search for files?