views:

8607

answers:

5

How can I get a tree of all the files from a current folder in Java?

+4  A: 
import java.io.File;
public class Test {
    public static void main( String [] args ) {
        File actual = new File(".");
        for( File f : actual.listFiles()){
            System.out.println( f.getName() );
        }
    }
}

It displays indistinctly files and folders.

See the methods in File class to order them or avoid directory print etc.

http://java.sun.com/javase/6/docs/api/java/io/File.html

OscarRyz
Your anchor link is broken (I guess the markup system assumes parentheses can't be in hyperlinks).
Michael Myers
Thanks. What about now?
OscarRyz
hehe.. I should probably RTFM more often.. :)
Lipis
If you put the link in a footnote, the one you had originally should actually work. (I know because I just did it in a different question.)
Michael Myers
Actually, just putting angle brackets around it ought to do the trick.
Michael Myers
+5  A: 

Check out Apache Commons FileUtils (listFiles, iterateFiles, etc.). Nice convenience methods for doing what you want and also applying filters.

http://commons.apache.org/io/api-1.4/org/apache/commons/io/FileUtils.html

Brandon DuRette
+5  A: 

Not sure how you want to represent the tree? Anyway here's an example which scans the entire subtree using recursion. Files and directories are treated alike. Note that listFiles() returns null for non-directories.

public static void main(String[] args) {
    final Collection<File> all = new ArrayList<File>();
    addFilesRecursively(new File("."), all);
    System.out.println(all);
}

private static void addFilesRecursively(File file, Collection<File> all) {
    final File[] children = file.listFiles();
    if (children != null) {
        for (File child : children) {
            all.add(child);
            addFilesRecursively(child, all);
        }
    }
}
volley
I can't remember how much times I have wrote this code. :-P
marcospereira
Yeah it's like a recurring nightmare.. :P~
volley
I have to accept this answer since I asked for the tree (I had accepted the Oscar Reyes' answer first).. even though adding one more line for the recursion wasn't that hard :)
Lipis
A: 

In JDK7, "more NIO features" should have methods to apply the visitor pattern over a file tree or just the immediate contents of a directory - no need to find all the files in a potentially huge directory before iterating over them.

Tom Hawtin - tackline
+1  A: 

You can also use the FileFilter interface to filter out what you want. It is best used when you create an anonymous class that implements it:

import java.io.File;
import java.io.FileFilter;

public class ListFiles {
    public File[] findDirectories(File root) { 
        return root.listFiles(new FileFilter() {
            public boolean accept(File f) {
                return f.isDirectory();
            }});
    }

    public File[] findFiles(File root) {
        return root.listFiles(new FileFilter() {
            public boolean accept(File f) {
                return f.isFile();
            }});
    }
}
Leonel