Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!nuzba.szn.dk!pnx.dk!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: Mon, 25 Feb 2013 21:53:56 +0100 Lines: 152 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> <512a993e$0$289$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 tZz3tVUfkmBcJ8bzklx1rwp2vwvCrWQGZW2RRva8nkYsGTWZo= Cancel-Lock: sha1:lBtnMHu1hnxySOp2YoU35CKqndg= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 In-Reply-To: <512a993e$0$289$14726298@news.sunsite.dk> Xref: csiph.com comp.lang.java.programmer:22514 On 24.02.2013 23:50, Arne Vajh=F8j wrote: > On 1/27/2013 7:55 AM, Robert Klemme wrote: >> 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 >> 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, >> 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, >> 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 >> SynchronousQueue>(); >> final CountFilterThread cf =3D new CountFilterThread(dir, >> 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.5fus/file.\n", >> TimeUnit.NANOSECONDS.toMicros(delta), entries.size(), >> (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 >> definitively not simpler than the straight forward approach. :-) > > Is that much different from the throw exception in filter solution > except that it requires a lot more code? No. robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/