4

After getting a list of the drive roots, is there a cross-platform way in Java to check whether any of the drives is:

  • A DVD drive
  • ...that contains a disk?

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?

Michael Berry
  • 70,193
  • 21
  • 157
  • 216

1 Answers1

6

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.

prunge
  • 22,460
  • 3
  • 73
  • 80
  • 1
    Bit of a hack though, really. But it's good for identifying immediately playable drives. However, wouldn't like to use it for permanently displaying a drive. – Chris Dennett Aug 12 '11 at 00:17
  • 4
    This isn't cross platform. On Linux using EXT4, the output is: /: rootfs – Jesan Fafon Aug 31 '12 at 16:03