Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: The first 10 files Date: Sun, 27 Jan 2013 13:55:18 +0100 Lines: 146 Message-ID: References: <51041ff8$0$284$14726298@news.sunsite.dk> <1iop8bl8ysrfg$.rdxcxhgxuj1r$.dlg@40tude.net> <5104925e$0$284$14726298@news.sunsite.dk> <51049469$0$293$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net fH51OllFODxLMAzyzcfE0AgBWslA8aUfYAlcVzLPv56ygz/QQpLbAUFGPIzrjNPo0= Cancel-Lock: sha1:Bi9n9F7JmNU2t6CsU5al+45hclA= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: <51049469$0$293$14726298@news.sunsite.dk> Xref: csiph.com comp.lang.java.programmer:21797 On 27.01.2013 03:43, Arne Vajh=F8j wrote: > On 1/26/2013 9:35 PM, Arne Vajh=F8j wrote: >> On 1/26/2013 9:02 PM, Arved Sandstrom wrote: >> 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 dir =3D > Files.newDirectoryStream(Paths.get("/work")).iterator(); > int n =3D 0; > while(dir.hasNext() && n < 10) { > System.out.println(dir.next()); > } > } > } For earlier Java versions we could emulate that with a second thread. package file; import java.io.File; import java.io.FileFilter; import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.TimeUnit; public final class ListFileTestThreaded2 { private static final class CountFilterThread extends Thread=20 implements FileFilter { private final File dir; private final int maxFiles; private final BlockingQueue> queue; private List filesSeen =3D new ArrayList(); public CountFilterThread(File dir, int maxFiles,=20 BlockingQueue> queue) { this.dir =3D dir; this.maxFiles =3D maxFiles; this.queue =3D queue; } @Override public void run() { try { dir.listFiles(this); if (filesSeen !=3D null) { send(); } } catch (InterruptedException e) { e.printStackTrace(); } } private void send() throws InterruptedException { queue.put(filesSeen); filesSeen =3D null; } @Override public boolean accept(final File f) { try { if (filesSeen !=3D null) { filesSeen.add(f); if (filesSeen.size() =3D=3D maxFiles) { send(); assert filesSeen =3D=3D null; } } return false; } catch (InterruptedException e) { throw new IllegalStateException(e); } } } private static final int[] LIMITS =3D { 10, 100, 1000, 10000,=20 Integer.MAX_VALUE }; public static void main(String[] args) throws InterruptedException { for (final String s : args) { System.out.println("Testing: " + s); final File dir =3D new File(s); if (dir.isDirectory()) { for (final int limit : LIMITS) { final SynchronousQueue> queue =3D new=20 SynchronousQueue>(); final CountFilterThread cf =3D new CountFilterThread(dir,=20 limit, queue); cf.setDaemon(true); final long t1 =3D System.nanoTime(); cf.start(); final List entries =3D queue.take(); final long delta =3D System.nanoTime() - t1; System.out.printf("It took %20dus to retrieve %20d files,=20 %20.5fus/file.\n", TimeUnit.NANOSECONDS.toMicros(delta), entries.size(),=20 (double) TimeUnit.NANOSECONDS.toMicros(delta) / entries.size()); } } else { System.out.println("Not a directory."); } } System.out.println("done"); } } https://gist.github.com/4648256 It's not guaranteed though that this will be faster. And it's=20 definitively not simpler than the straight forward approach. :-) Cheers robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/