Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I check the size of a collection as it is being populated

I'm listing the files on a file system in Java using the following Apache Commons class:

Collection<File> allFiles = FileUtils.listFiles(rootDirectory, null, recursive);

This is a very long process, and can take up to 5 minutes.

Is there any way I can check the size of the collection while it is being populated?

I tried accessing it from a separate thread but simply got zero until the process was ready.

like image 661
Redandwhite Avatar asked Sep 03 '12 10:09

Redandwhite


1 Answers

Until you get a return value from the method, you won't be able to access the collection as it gets populated, because you don't have access to the variable that this method uses internally.

You could split the search in smaller pieces by not using the recursive flag and deal with the recursion yourself to receive a collection per directory.

A better alternative, if you are using Java 7, is to use the FileVisitor interface to explore the file system. It has a few callback methods that enable you to track the progress.

like image 155
assylias Avatar answered Oct 14 '22 20:10

assylias