views:

717

answers:

11

Hi. I'd like to do a search for folders/directories from java, and go into those folders/directories in java. I guess it's called system utilities? Any tutorials out there, or books on the subject?

Thanks ;)

A: 

You can use java.io.File class to search.

Dmitry Khalatov
+6  A: 

I use this code to get all ZIP files in a folder. Call this recursively checking for the file object to be a sub directory again and again.

public List<String> getFiles(String folder) {

List<String> list = new ArrayList<String>();
        File dir = new File(folder);
        if(dir.isDirectory()) {
            FileFilter filter = new FileFilter() {

                public boolean accept(File file) {
                    boolean flag = false;
                    if(file.isFile() && !file.isDirectory()) {
                        String filename = file.getName();
                        if(!filename.endsWith(".zip")) {
                            return true;
                        }
                        return false;   
                }

            };
            File[] fileNames = dir.listFiles(filter);
            for (File file : fileNames) {
                list.add(file.getName());
            }
            return list;

}

Sandy
Why are you using Strings / the filename instead of File objects?
Tim Büthe
The above code sample is from one of my module's where we needed to generate only the filenames of all ZIP files in a folder, and hence, it made more sense to keep it as String and pass that to a webservice for sanity check. This allowed us to keep the footprint low.
Sandy
+2  A: 

If you want to navigate the file system, take a look at File and the list() method. You'll most likely require some recursive method to navigate down through the hierarchies.

Brian Agnew
+1  A: 

I don't know of any tutorials or books on that particular subject, but the way to do it is to use the java.io.File class. For example, you can use the list() to get a list of the contents of a directory. Then it's just a matter of using isDirectory() and recursing to search an entire file tree.

Adam Rosenfield
+4  A: 

You could use Apache Commons FileUtils (see: http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html) and specifically the listFiles method there, which can do this recursively and use filters (so it saves you the writing of the recursion yourself and answers the search you mentioned).

Gadi
+2  A: 

I'd recommend Apache Commons IO utilities.

vartec
A: 

Thanks :D The File class was just what I was looking for..! :)

Johannes
A: 

here is another example:

    for (File file : File.listRoots()[0].listFiles()) {
        System.out.println(file);
    }

the same, printing only directories:

    FileFilter isDirectory = new FileFilter() {

        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    };

    for (File file : File.listRoots()[0].listFiles(isDirectory)) {
        System.out.println(file);
    }
dfa
A: 

Good example: http://www.leepoint.net/notes-java/io/10file/20recursivelist.html

BTW. I recommend reading the whole thing. http://www.leepoint.net/notes-java/

vartec
+1  A: 

I used Apache Commons VFS.

Is nice to use it for read contents of a directory, like this:

FileSystemManager fsManager = VFS.getManager();
FileObject path = fsManager.resolveFile( "file:///tmp" );

FileObject[] children = path.getChildren();
System.out.println( "Children of " + path.getName().getURI() );
for ( int i = 0; i < children.length; i++ )
{
    System.out.println( children[ i ].getName().getBaseName() );
}

You can check if children is file, folder or something different with getType().

And same code works for reading ZIP or JAR files, FTP, SFTP, ... just changing the URL of resolveFile as you can see here.

SourceRebels
A: 

is there an algorithm to walk a directory non-recursively?

vkolodrevskiy