Hi, I'm currently writing a class called FileIterator that implements the Iterator interface and enables to iterate over all files, dirs, files in those dirs etc. in a specific directory. As you can see, you can provide a FileFilter or a FilenameFilter to filter files you don't want to be returned. The strange thing is: if I specify a filter that somehow invokes a method on one of FileFilter or FilenameFilter's accept() args, a StackOverflowError is thrown(see WorkingFileiteratorTest and NotWorkingFileIteratorTest below). What's even more strange is is the fact, that the StackOverflowError is not thrown immediately and, if the number of files in the specified directory is small enough, everything works just fine. Sorry for the long listing, you don't have to read the whole code, the interesting parts lie in WorkingFileiteratorTest and NotWorkingFileIteratorTest.
NotWorkingFileiteratorTest.java(Throws a StackOverflowError after having found some files):
Sorry for this long post and thanks if vou'ye managed to get this far
If the filter returns true, then we return "lastFile", and everything's OK. But if the filter returns "false", then this method calls itself recursively. If there are many non-matching files, then you'll get many recursive calls, and eventually you'll run out of stack space and get the exception.
You could rewrite it non-recursively something like this:
You are my hero I suspected the problem to be somewhere completely else. I have to rewrite it nevertheless, because hasNext doesn't work properly. This class was a work in progress and the ability to add Filters was not included in the first running version, so it isn't suited for being able to filter files(I noticed that while searching for the error's cause after having opened this thread). I'll rewrite it and post it in here, maybe someone finds it useful(I definitely think it is).
Thank you very much, Michael Herrmann [ September 26, 2004: Message edited by: Michael Herrmann ]
Joined: Dec 06, 2003
Here is the rewritten version that doesn't produce the StackOverflowError mentioned above when traversing my whole HDD(122.000 files, 12.000 directories) or when FileIterator.remove()-ing a directory that contains 23.000 files in 1.600 directories: