1

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

Community
  • 1
  • 1
SaQiB
  • 639
  • 8
  • 18
  • 1
    I think Pauls suggestion below is more elegant than the above link, but that is just my opinion – Kennet Jan 16 '12 at 09:22

1 Answers1

2

One solution could be to use a FileNameFilter that either returns false after a certain number of files have been accepted or, if you are prepared to abuse Exceptions a little, throw an Exception at that point.

class CountedFilter implements FilenameFilter {
  int limit = 0;

  public CountedFilter ( int limit ) {
    this.limit = limit;
  }

  public boolean accept(File file, String string) {
    return limit-- > 0;
  }

}


There is another option which is nowhere near as succinct but I believe is more effective. It involves a FilenameFilter that posts each file back to the caller through a BlockingQueue. The File.list is 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.list is running producing a much smoother solution, much like an iterator. If encouraged I will post the code as a separate answer.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • i am not sure but i see no benefit using FileNameFilter ..actually the files types in the directory are of same type and filenames are random based on datetime, so how can i use this FileNameFilter.. – SaQiB Jan 16 '12 at 09:22
  • that looks promising, let me try this. thanks for now. – SaQiB Jan 16 '12 at 09:28
  • Note that this will not deliver the files in any predictable order. To select just the oldest files you will have to list all of them. – OldCurmudgeon Jan 16 '12 at 09:34
  • i think it won't be any issue as i am not concerned with the sequence and my function deletes that file after execution – SaQiB Jan 16 '12 at 09:37