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...

[755 byte] By [the_blackeninga] at [2007-11-26 14:43:45]
# 1
Exuse me,The statement that throw the Exception is 3 and not 2.Thanks.
the_blackeninga at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 2
Statement 2 can't throw an IndexOutOfBoundsException
kajbja at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 3
Of course, itś the STATEMENT 3
the_blackeninga at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 4
> Exuse me,> The statement that throw the Exception is 3 and not> 2.> Thanks.Ok. That statement will throw an exception if the array is of length zero (which is != null)Kaj
kajbja at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 5

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?

the_blackeninga at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 6
I think that your filter is not working the way you think, because the only way you get the error you are getting is if the array length is 0.~Tim
SomeoneElsea at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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"

kajbja at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 8

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_blackeninga at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 9
And the only file that match with the filter is a.txt.
the_blackeninga at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 10
and yet you still have an array with 0 files. something that you think is working, isnt.
mkoryaka at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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

SomeoneElsea at 2007-7-8 8:31:24 > top of Java-index,Java Essentials,Java Programming...