简体   繁体   中英

Get all DVD drives in Java

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM