Walking a Linux/Unix filesystem with Java?

自古美人都是妖i 提交于 2020-01-30 08:21:07

问题


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?


回答1:


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.




回答2:


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

    }
}



回答3:


In JDK7, the new package java.nio.file provides useful utilities around files. In particular, the java.nio.file.Files class has walkFileTree(..) methods and other file-related utility functions.

See JDK7 API Doc: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html




回答4:


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



来源:https://stackoverflow.com/questions/1924447/walking-a-linux-unix-filesystem-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!