views:

130

answers:

3

Hello, I'm writing a program that has to mantain a large catalog of files. Those may be on different devices and I'd like to know what is the best way to do this in Java. I mean that I need to:

  • Identify the device the file (or just a directory) is on and get a name or identifier for this device that will be consistent...
  • Be able to use this device identifier to check if the device is plugged (like a USB pen, a HDD mounted or not, a network drive...)
  • Having this compatible with Windows, Unix and whatever...

The ideal solution would be some kind of device abstraction... In the docs I can see that File is related to partitions but it doesn't seem to be a proper abstraction or object of the filesystem. Or maybe if there is not a proper java abstraction, the best way to handle this diversity on each system.

+1  A: 

Java 7 has the Path class which is an improvement over File. Check it out at http://download.java.net/jdk7/binaries/, or the tutorial at http://java.sun.com/docs/books/tutorial/essential/io/fileio.html.

Beau Martínez
For the moment I'd like to work with Java 6. Java 7 isn't yet released and I'd like to struck on portable stuff
gotch4
A: 

If it is acceptable to (a) run only on Windows and (b) require admin rights to run your program, then this can be done with WMI via JACOB.

In particular take a look at the Win32_LogicalDisk class. This is sufficient to get the drive type (IDE, network, USB etc), the volume label and volume serial number (which may be enough to uniquely identify a removable volume for the purposes of your application.)

For more advanced functions (e.g. getting drive serial numbers) there are other classes, e.g. Win32_DiskDrive, Win32_DiskPartition, Win32_PhysicalMedia. However these will only work fully if running as admin.

finnw
A: 

A quick google turned up http://today.java.net/article/2006/07/05/java-and-usb which may be of help. At least it describes the state of USB support from a few years ago.

I would attempt to largely avoid the problem, by simplifying aspects of it. Namely I would would keep your catalog at the file/folder abstraction and then use the above article to generate code that listens for any USB activity.

Of course such an approach would require you to check a specific file/folder exists prior to the loading that portion of your catalog. Also you would require a thread to constantly listen for USB events and then perform the neccessary checks if any USB device has been connected/disconnected and perform the neccessary checks on the filesystem again.