4

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 ;)

Kara
  • 6,115
  • 16
  • 50
  • 57

9 Answers9

8

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;

}

sangupta
  • 2,396
  • 3
  • 23
  • 37
  • Why are you using Strings / the filename instead of File objects? – Tim Büthe Apr 16 '09 at 12:32
  • 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. – sangupta Apr 17 '09 at 05:12
5

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
  • 472
  • 4
  • 10
2

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
  • 268,207
  • 37
  • 334
  • 440
2

I'd recommend Apache Commons IO utilities.

vartec
  • 131,205
  • 36
  • 218
  • 244
1

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
  • 390,455
  • 97
  • 512
  • 589
0

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

Dmitry Khalatov
  • 4,313
  • 3
  • 33
  • 39
0

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
  • 114,442
  • 31
  • 189
  • 228
0

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
  • 131,205
  • 36
  • 218
  • 244
0

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
  • 5,140
  • 1
  • 32
  • 52