Counting the number of files in a directory using Java

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.

Leave a Comment