Get all DVD drives in Java

馋奶兔 提交于 2020-01-03 16:22:58

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7034216/get-all-dvd-drives-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!