用Java获取所有DVD驱动器 [英] Get all DVD drives in Java
问题描述
获得清单后是否有一个跨平台的方式在Java中检查是否有任何驱动器是:
- A DVD光驱
- ...包含磁盘?
我希望用户成为能够选择DVD播放,而将选项缩小到DVD驱动器,而不是包括其他驱动器(如笔式驱动器,硬盘驱动器等)将有助于在这种情况下。如果我能得到这样的驱动器列表,显示哪些包含磁盘将再次有帮助(同样的原因)。
经过搜索,但我还没有找到任何方式做到这一点,不涉及平台特定的hackery。有没有什么东西在那里?
解决方案新的 file system API 可以这样做:
FileSystem fs = FileSystems.getDefault(); (PathPath:fs.getRootDirectories())
尝试
{
FileStore store = Files.getFileStore(rootPath);
System.out.println(rootPath +:+ store.type());
catch(IOException e)
{
System.out.println(rootPath +:+< error getting store details>);
在我的系统中,它提供了以下内容在驱动器D中,其余的硬盘或网络共享):
C:\:NTFS
D:\\ \\:CDFS
H:\:NTFS
M:\:NTFS
S:\:NTFS
T:\:NTFS
V:\\ \\:<获取商店详情的错误>
W:\:NTFS
Z:\:NTFS
对于文件存储的 type()查询应该这样做。
如果CD不在驱动器中,getFileStore() / b>
java.nio.file.FileSystemException:D ::设备未准备好。
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.
这篇关于用Java获取所有DVD驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!