Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Walking a Linux/Unix filesystem with Java?

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?

like image 820
flesh Avatar asked Apr 12 '26 03:04

flesh


1 Answers

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.

like image 67
Brian Agnew Avatar answered Apr 14 '26 15:04

Brian Agnew