Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah..that is exactly what i did, only I didn't check your reply on time

Many thanks regardless
 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic