views:

325

answers:

3

I need to create a Java util that will recurse its way through a Unix (and/or Linux) filesystem and build an object model of the directory structure, retrieve file info - size, created date, last accessed date etc - plus I need to retrieve info on the physical storage device the files are sitting on. Ideally, this util will be portable. I have no experience of the Java standard libraries and only limited experience of Unix OS.

Are there Java standard libraries that will handle working with the Unix filesystem? Or am I going to have to make native calls through some API and then worry about portability? What options do I have?

+2  A: 

You can use: java.io.File for that it may not contain all the information you need ( like the information of the physical storage ) but it will help you to create most of the logic you need.

You may take a look at muucommander which is an open source file manager. They've already done a lot of work to make this kind of stuff, and the code is very clean and accesible.

Here's a working sample:

import java.io.File;


public class FileWalkTest {
    public static void main(String[] args) {
        File file = new File("/Users/oscarreyes/code/");
        printInfo( file );
    }
    public static void printInfo( File dir ) {
        for( File child : dir.listFiles() ) {
            if( child.isDirectory() ) {
                System.out.println( "\n"+child.getAbsolutePath());
                printInfo( child );
            } else {
                System.out.printf("%s read:%s write:%s xecute:%s, size:%d lastModified:%d %n", child.getName(),  child.canRead(), child.canWrite(), child.canExecute(), child.length(), child.lastModified());
            }
       }
        System.out.println();

    }
}
OscarRyz
+3  A: 

Check out Apache Commons IO and FileUtils.iterator() in particular. That'll allow you to navigate the file system. Using an iterator is better than building up a huge list of candidate files mainly because of the potential memory issues.

If you need particular access to symlinks, then this may not be enough, and you may want to check out an early release of JDK 7. I understand the file system with Java 7 will have some capabilities surrounding symlinks.

Note: many Unix filesystems will give you ctime, which is the inode creation date and not the file creation date.

Brian Agnew
Thanks Brian. Do you know of any resources on the limitations (like *ctime* cited above for example) of Java IO over Unix filesystems? In general, for fairly basic operations, is Java as good as anything else for walking a Linux/Unix filesystem?
flesh
I would check out the File object in Java to determine if that does what you want. For *really* low-level file system interrogation on Unix, I would perhaps suggest Perl. It has access to the system-level stats that Java simply won't (by virtue of the JVM and its cross-platform nature)
Brian Agnew
+2  A: 

java.io.File is your friend here. It has a number of methods that allow you to list the contents of a directory and determine whether what it finds is a file or a directory. If it's a directory then you just need to recurse down in to it. Once you know the object you've got is a file then there are methods you can call to get specific details.

Have a look at http://java.sun.com/j2se/1.5.0/docs/api/

I believe that this entirely portable as well

Dave