Skip to main content

scanning folders in Java using foreach loop

8 replies [Last post]
vpatryshev
Offline
Joined: 2004-06-30
Points: 0

I think I got it. Use double loop. No recursion necessary (it is hidden in tree() method).

<br />
import static com.myjavatools.lib.Files.*;</p>
<p>..........</p>
<p>    for (File dir : tree(new File("."))) {<br />
      System.out.println(dir);<br />
      for (File f : files(dir)) {<br />
        System.out.println("  " + f);<br />
      }<br />
    }</p>
<p>

Check out http://myjavatools.com - lib, version 5.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
s690716
Offline
Joined: 2004-03-04
Points: 0

There is an open RFE since 28-OCT-1999:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4285834

Evaluation:
Incremental reads of large directories will be addressed in the new I/O
framework. -- xxxxx@xxxxx 1999/10/28

OK, the new I/O API is available... but...

(And don't forget http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4057701)

vpatryshev
Offline
Joined: 2004-06-30
Points: 0

Oh. Thanks a lot.

By the way, the article is here now: http://www.devx.com/Java/Article/27367

umdenken
Offline
Joined: 2004-11-16
Points: 0

You might find my javafind project interesting. I've implemented many of the Unix find features:

https://sourceforge.net/projects/javafind/

vpatryshev
Offline
Joined: 2004-06-30
Points: 0

Cool! It is a dedicated tool, with all the features; I would not mix it with the lightweight version of find() in Files.java. But thanks anyway!

tackline
Offline
Joined: 2003-06-19
Points: 0

What's the traversal order of tree()? Depth first, bredth first, descendent nearest the head in the same direction or something more obscure?

I haven't had a praticular need for a flat list of directories with nested files. I don't have any applications in mind, but I think I'd go for a visitor pattern (accepting directory, normal files and others) rather than try to bring iteration management into the client code.

vpatryshev
Offline
Joined: 2004-06-30
Points: 0

Depth. I feel like it is the natural order in the case of file system. Preorder or postorder, you decide.

I'd never trade a simple loop for vistitor. You know why? Imagine that instead of directory structure you have a zip file - is there any tree at all? No. Just an imaginary tree.

I mean, the notion of "directory tree" is an artifact. It is not a natural phenomenon. It depends on how you see it.

By the way, do you think there is any point in scanning order? :) Say, in a zip file - do you care about file order?

tackline
Offline
Joined: 2003-06-19
Points: 0

I was curious as to why you used two loops, when a method that returned all the files (including directories and non-normal files) as a Collection would require only a single loop.

I guess it kind of works as a poor man's visitor. How does you code work for a ZIP archive if you're ignoring the 'imaginary' directories?

vpatryshev
Offline
Joined: 2004-06-30
Points: 0

See, double loop may be useful if, for instance, you gather information from the files in the folder, and then write a summary file. There are two traversal orders to choose: preorder (default) or postorder.

But, on the other hand, I'm thinking of a way of combining the two iterables into one (the methods are already there, but the resulting expression looks too long to be beautiful).