Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Wojtek Newsgroups: comp.lang.java.programmer Subject: The first 10 files Date: Sat, 26 Jan 2013 01:14:18 -0800 Organization: A noiseless patient Spider Lines: 23 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15"; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: mx04.eternal-september.org; posting-host="92c3b945ba89fdf5072f2811ba779245"; logging-data="22266"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19T8+2h0dyJRA+A2Z0GBz1z5TuPJIXrVPE=" X-Newsreader: MesNews/1.08.03.00-gb Cancel-Lock: sha1:ybzftOBtBYSwhRfIxYrQ+mQb7IQ= Xref: csiph.com comp.lang.java.programmer:21727 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? -- Wojtek :-)