Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #22514

Re: The first 10 files

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: The first 10 files
Date 2013-02-25 21:53 +0100
Message-ID <ap21b9F52mbU2@mid.individual.net> (permalink)
References (7 earlier) <iV%Ms.125649$Id.75544@newsfe24.iad> <5104925e$0$284$14726298@news.sunsite.dk> <51049469$0$293$14726298@news.sunsite.dk> <amkmdqFlumnU1@mid.individual.net> <512a993e$0$289$14726298@news.sunsite.dk>

Show all headers | View raw


On 24.02.2013 23:50, Arne Vajhøj wrote:
> On 1/27/2013 7:55 AM, Robert Klemme wrote:
>> On 27.01.2013 03:43, Arne Vajhøj wrote:
>>> On 1/26/2013 9:35 PM, Arne Vajhøj 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<Path> dir =
>>> Files.newDirectoryStream(Paths.get("/work")).iterator();
>>>          int n = 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<List<File>> queue;
>>      private List<File> filesSeen = new ArrayList<File>();
>>
>>      public CountFilterThread(File dir, int maxFiles,
>> BlockingQueue<List<File>> queue) {
>>        this.dir = dir;
>>        this.maxFiles = maxFiles;
>>        this.queue = queue;
>>      }
>>
>>      @Override
>>      public void run() {
>>        try {
>>          dir.listFiles(this);
>>
>>          if (filesSeen != null) {
>>            send();
>>          }
>>        } catch (InterruptedException e) {
>>          e.printStackTrace();
>>        }
>>      }
>>
>>      private void send() throws InterruptedException {
>>        queue.put(filesSeen);
>>        filesSeen = null;
>>      }
>>
>>      @Override
>>      public boolean accept(final File f) {
>>        try {
>>          if (filesSeen != null) {
>>            filesSeen.add(f);
>>
>>            if (filesSeen.size() == maxFiles) {
>>              send();
>>              assert filesSeen == null;
>>            }
>>          }
>>
>>          return false;
>>        } catch (InterruptedException e) {
>>          throw new IllegalStateException(e);
>>        }
>>      }
>>    }
>>
>>    private static final int[] LIMITS = { 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 = new File(s);
>>
>>        if (dir.isDirectory()) {
>>          for (final int limit : LIMITS) {
>>            final SynchronousQueue<List<File>> queue = new
>> SynchronousQueue<List<File>>();
>>            final CountFilterThread cf = new CountFilterThread(dir,
>> limit, queue);
>>            cf.setDaemon(true);
>>            final long t1 = System.nanoTime();
>>            cf.start();
>>            final List<File> entries = queue.take();
>>            final long delta = 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



-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

The first 10 files Wojtek <nowhere@a.com> - 2013-01-26 01:14 -0800
  Re: The first 10 files Roedy Green <see_website@mindprod.com.invalid> - 2013-01-26 02:44 -0800
    Re: The first 10 files Lew <lewbloch@gmail.com> - 2013-01-26 10:20 -0800
  Re: The first 10 files "John B. Matthews" <nospam@nospam.invalid> - 2013-01-26 06:31 -0500
    Re: The first 10 files Wojtek <nowhere@a.com> - 2013-01-26 15:42 -0800
      Re: The first 10 files Jim Janney <jjanney@shell.xmission.com> - 2013-01-26 17:13 -0700
      Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-26 21:21 -0500
      Re: The first 10 files "John B. Matthews" <nospam@nospam.invalid> - 2013-01-26 22:05 -0500
  Re: The first 10 files Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-26 08:24 -0400
    Re: The first 10 files Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-26 08:25 -0400
    Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-26 13:26 -0500
      Re: The first 10 files Robert Klemme <shortcutter@googlemail.com> - 2013-01-26 22:15 +0100
        Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-26 16:25 -0500
        Re: The first 10 files Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-26 17:06 -0500
          Re: The first 10 files Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-26 15:21 -0800
            Re: The first 10 files Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-26 20:42 -0500
              Re: The first 10 files Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-26 17:56 -0800
                Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-26 21:29 -0500
                Re: The first 10 files Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-26 21:56 -0500
                Re: The first 10 files Jim Janney <jjanney@shell.xmission.com> - 2013-01-26 20:51 -0700
                Re: The first 10 files Jim Janney <jjanney@shell.xmission.com> - 2013-01-26 20:47 -0700
              Re: The first 10 files Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-26 22:02 -0400
                Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-26 21:35 -0500
                Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-26 21:43 -0500
                Re: The first 10 files Robert Klemme <shortcutter@googlemail.com> - 2013-01-27 13:55 +0100
                Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-02-24 17:50 -0500
                Re: The first 10 files Robert Klemme <shortcutter@googlemail.com> - 2013-02-25 21:53 +0100
                Re: The first 10 files Jim Janney <jjanney@shell.xmission.com> - 2013-01-26 20:57 -0700
                Re: The first 10 files Wojtek <nowhere@a.com> - 2013-01-26 21:20 -0800
                Re: The first 10 files Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-27 07:23 -0400
                Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-27 20:36 -0500
                Re: The first 10 files Wojtek <nowhere@a.com> - 2013-01-28 16:28 -0800
            Re: The first 10 files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-26 21:23 -0500
            Re: The first 10 files Roedy Green <see_website@mindprod.com.invalid> - 2013-01-26 19:09 -0800
  Re: The first 10 files Jim Janney <jjanney@shell.xmission.com> - 2013-01-26 16:00 -0700
  Re: The first 10 files Knute Johnson <nospam@knutejohnson.com> - 2013-01-26 18:37 -0800
    Re: The first 10 files Wojtek <nowhere@a.com> - 2013-03-14 03:07 -0700
      Re: The first 10 files lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-03-14 12:49 +0000
      Re: The first 10 files Robert Klemme <shortcutter@googlemail.com> - 2013-03-15 11:38 +0100
        Re: The first 10 files Wojtek <nowhere@a.com> - 2013-03-15 10:31 -0700

csiph-web