Reputation: 72284
After getting a list of the drive roots, is there a cross-platform way in Java to check whether any of the drives is:
I want the user to be able to select a DVD for playing, and narrowing the options down to DVD drives rather than including other drives (such as pen drives, hard drives etc.) would be helpful in this case. If I can get a list of such drives, showing what ones contain disks would again be helpful (same reason.)
After searching around though I haven't found any way to do this that doesn't involve platform-specific hackery. Is there anything out there?
Upvotes: 4
Views: 2435
Reputation: 9543
Here's a Linux-compatible approach:
FileSystem fs = FileSystems.getDefault();
for (FileStore store : fs.getFileStores()) {
String storeString = store.toString();
String type = store.type();
if (type.equals("tmpfs")) {
continue;
}
if (storeString.startsWith("/dev")) {
continue;
} else if (storeString.startsWith("/proc")) {
continue;
} else if (storeString.startsWith("/sys")) {
continue;
} else if (storeString.startsWith("/run/") && !storeString.startsWith("/run/user")) {
continue;
}
int index = storeString.indexOf(" (");
if (index < 0) {
continue;
}
String path = storeString.substring(0, index);
System.out.printf("%-30s %-20s %-10s\n", path, store.name(), type);
}
It can only detect a *disk*, not a drive. With no disk inserted, or a corrupted disk, the mount point just disappears.
/ /dev/sda5 btrfs
/boot/efi /dev/sda1 vfat
/mnt/oldubuntu /dev/sda3 ext4
/home /dev/sda4 ext4
/run/user/1000/gvfs gvfsd-fuse fuse.gvfsd-fuse
/run/user/1000/doc portal fuse.portal
/media/zom-b/W1200A /dev/sdb1 ext4
/mnt/nasu [email protected]:/ fuse.sshfs
/media/zom-b/MovieDataDVD001 /dev/sr0 iso9660
Optical disks can be iso9660
or udf
afaik
Upvotes: 0
Reputation: 23248
The new file system API in Java 7 can do this:
FileSystem fs = FileSystems.getDefault();
for (Path rootPath : fs.getRootDirectories())
{
try
{
FileStore store = Files.getFileStore(rootPath);
System.out.println(rootPath + ": " + store.type());
}
catch (IOException e)
{
System.out.println(rootPath + ": " + "<error getting store details>");
}
}
On my system it gave the following (with a CD in drive D, the rest hard disk or network shares):
C:\: NTFS
D:\: CDFS
H:\: NTFS
M:\: NTFS
S:\: NTFS
T:\: NTFS
V:\: <error getting store details>
W:\: NTFS
Z:\: NTFS
So a query on the file store's type() should do it.
With a CD not in the drive, the getFileStore() call throws
java.nio.file.FileSystemException: D:: The device is not ready.
Upvotes: 6