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


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

Re: The first 10 files

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: The first 10 files
Date 2013-01-26 18:37 -0800
Organization A noiseless patient Spider
Message-ID <ke23sm$afb$1@dont-email.me> (permalink)
References <mn.d04a7dd156c148ef.70216@a.com>

Show all headers | View raw


On 1/26/2013 1:14 AM, Wojtek wrote:
> Using:
>
> int max = 10;
> int count = 0;
>
> for (File thisFile : aDir.listFiles())
> {
>   doSomething(thisFile);
>
>   if ( ++count >= max )
>     break;
> }
>
> gives me the first ten files in aDir. But if aDir contains 30K files,
> then the listFiles() will run for a long time as it builds an array for
> the 30K files.
>
> Is there a way to have Java only get the first "max" files?
>

import java.io.*;
import java.nio.*;
import java.nio.file.*;

public class FileSystemsTest {
     public static void main(String[] args) throws IOException {
         long start = System.currentTimeMillis();
         Path dir = FileSystems.getDefault().getPath(".");
         int i=10;
         DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
         for (Path path : stream) {
             System.out.println(path.getFileName());
             if (--i <= 0)
                 break;
         }
         long stop = System.currentTimeMillis();
         System.out.println(stop - start);
     }
}

300003 files in the directory, almost 1.7GB of files, Windows XP, Java 7 
and it takes 16 ms to run.  Somebody else should try this out on their 
computer to see if it works as fast.

.
.
.
01/26/2013  05:46 PM            58,890 9998.txt
01/26/2013  05:46 PM            58,890 9999.txt
01/26/2013  06:31 PM             1,316 FileSystemsTest.class
01/26/2013  06:29 PM               636 FileSystemsTest.java
01/26/2013  05:44 PM               650 MakeFiles.java
            30003 File(s)  1,766,702,602 bytes
                2 Dir(s)  49,387,085,824 bytes free

C:\Documents and Settings\Knute Johnson\bigdirectory>java FileSystemsTest
0.txt
1.txt
10.txt
100.txt
1000.txt
10000.txt
10001.txt
10002.txt
10003.txt
10004.txt
16

C:\Documents and Settings\Knute Johnson\bigdirectory>


-- 

Knute Johnson

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