Possible Duplicate:
Is there a workaround for Java's poor performance on walking huge directories?
I created a utility which lists all the files within some directory by using File.listFiles() function, but the problem occurred when i copied huge amount of files (around 100K) under that directory.
I was looking for some solution take actually takes some fixed no of files (say X) from that directory and processes and then deletes those files and takes next X number of files.
Can any one please suggest me any solution.
Regards
One solution could be to use a
FileNameFilterthat either returnsfalseafter a certain number of files have been accepted or, if you are prepared to abuse Exceptions a little, throw anExceptionat that point.There is another option which is nowhere near as succinct but I believe is more effective. It involves a
FilenameFilterthat posts each file back to the caller through aBlockingQueue. TheFile.listis called in a separate thread so you don’t have to wait for it to complete.This solution has the added benefit that the files can be processed while
File.listis running producing a much smoother solution, much like an iterator. If encouraged I will post the code as a separate answer.