views:

4769

answers:

4

How do I count the number of files in a directory using Java ? For simplicity, lets assume that the directory doesn't have any sub-directories.

I know the standard method of :

new File(<directory path>).listFiles().length

But this will effectively go through all the files in the directory, which might take long if the number of files is large. Also, I don't care about the actual files in the directory unless their number is greater than some fixed large number (say 5000).

I am guessing, but doesn't the directory (or its i-node in case of Unix) store the number of files contained in it. If I could get that number straight away from the file system, it would be much faster. I need to do this check every time on a Tomcat server before the back-end starts doing the real processing for the http request. Therefore, speed is of paramount importance.

I could run a daemon every once in a while to clear the directory. I know that. So plz don't give me that solution.

+4  A: 

Unfortunately, I believe that is already the best way (although list() is slightly better than listFiles(), since it doesn't construct File objects).

Michael Myers
+11  A: 

Ah... the rationale for not having a straightforward method in Java to do that is file storage abstraction: some filesystems may not have the number of files in a directory readily available... that count may not even have any meaning at all (see for example distributed, P2P filesystems, fs that store file lists as a linked list, or database-backed filesystems...). So yes,

new File(<directory path>).list().length

is probably your best bet.

Varkhan
+1  A: 

Unfortunately, as mmyers said, File.list() is about as fast as you are going to get using Java. If speed is as important as you say, you may want to consider doing this particular operation using JNI. You can then tailor your code to your particular situation and filesystem.

Sebastian Celis
+1  A: 

This might not be appropriate for your application, but you could always try a native call (using jni or jna), or exec a platform-specific command and read the output before failing back to list().length. On *nix, you could exec ls -1a | wc -l (note - that's dash-one-a for the first command, and dash-lowercase-L for the second). Not sure what would be right on windows - perhaps just a dir and look for the summary.

Before bothering with something like this I'd strongly recommend you create a directory with a very large number of files and just see if list().length really does take too long. As this blogger suggests, you may not want to sweat this.

I'd probably go with Varkhan's answer myself.

Marty Lamb