listFiles with filter & memory problems
Hi,
First of all, excuse me, because my english is not so good...
I have some problems with java.io.File.listFiles (FileFilter filter) method when directories contents a lot of files (150.000 files more or less, it depends the machine)
Here is an example:
1) Dst = (new java.io.File(dataDirBase)).listFiles(filter);
2) if (Dst !=null)
3)b = Dst [0] ;
If the dataDirBase has 150.000 files then the statement 2 gives me an arrayIndexOutOfBoundsException:0. I can't understand how index 0 gives me an Exception when arrray isn't null. I could understand this if the error been OutOfMemoryError, but not this error.
Any idea?
Thanks in advance,
Machine Head - The Blackening coming soon...
Ok an array can be not null and its length not zero, thanks.
But how is posible array's length zero if I have at least one file that match with the filter?
I think listFiles returns an unresasonable result because of the quantity of the files.
What is your opinion about this?
> Ok an array can be not null and its length not zero,
> thanks.
> But how is posible array's length zero if I have at
> least one file that match with the filter?
> I think listFiles returns an unresasonable result
> because of the quantity of the files.
> What is your opinion about this?
It sounds like you filter all files, and that's the reason that the array is of zero length. Add print statements to all files that you are "keeping"
The filter is ok because it works perfectly when I have few files.
The file a.txt match with the filter, well, if I had in the directory 10 thousand files and the a.txt file it works good. But if I had 150 thousand files in the same directory and the file a.txt it works bad.
Thanks to all.
> The filter is ok because it works perfectly when I
> have few files.
> The file a.txt match with the filter, well, if I had
> in the directory 10 thousand files and the a.txt file
> it works good. But if I had 150 thousand files in the
> same directory and the file a.txt it works bad.
>
> Thanks to all.
I have a hard time believing that listing a files with 150000 files would cause java to barf like you are describing. More likely would be a StackOverflow (if recursive calls are being done) or an OutOfMemory Error. Do what was suggested, and in the filefilters accept method, do something like
public boolean accept(File f)
{
boolean result = f.getName().endsWith(".txt");
if(result)
System.out.println(f.getName() + " added to list of files to return");
return result;
}
~Tim
Typo