Determining physical file location and device status in Java

I am writing a program that needs to contain a large directory of files. They can be on different devices and I would like to know what is the best way to do it in Java. I mean what I need is:

  • Identify the device that contains the file (or just the directory) and get the name or ID for that device to be matched ...
  • You can use this device ID to check if a device is connected (e.g. USB pen, hard drive installed or not, network drive ...)
  • Compatible with Windows, Unix and others ...

The ideal solution is some kind of device abstraction ... In the docs I can see that the file is associated with partitions, but it doesn't seem to be a proper abstraction or filesystem object. Or maybe, if there is no corresponding java abstraction, there is a better way to deal with this diversity on every system.

+2


source to share


3 answers


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



+1


source


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

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



For more advanced functions (for example, getting serial numbers of drives) there are other classes, for example. Win32_DiskDrive, Win32_DiskPartition, Win32_PhysicalMedia. However, they will only work when run as administrator.

0


source


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

I would try to largely avoid the problem by simplifying aspects of it. Namely, I would store your directory in a file / folder abstraction and then use the above article to generate code that listens for any USB activity.

Of course, this approach will require you to check a specific file / folder before loading that part of your directory. You will also need a thread to constantly listen for USB events and then perform the necessary checks if any USB device was connected / disconnected and perform the necessary file system checks again.

0


source







All Articles