如何获取列表所有驱动器,但也获得相应的驱动器类型(可移动,本地磁盘,或CD-ROM,DVD-ROM等)?
paf*_*ivi 23
使用此代码,您可以获取所有驱动器及其类型说明
File[] paths;
FileSystemView fsv = FileSystemView.getFileSystemView();
// returns pathnames for files and directory
paths = File.listRoots();
// for each pathname in pathname array
for(File path:paths)
{
// prints file and directory paths
System.out.println("Drive Name: "+path);
System.out.println("Description: "+fsv.getSystemTypeDescription(path));
}
Run Code Online (Sandbox Code Playgroud)
小智 5
此代码段适用于 Windows。它正确地将 USB 笔式驱动器报告为可移动,但在我的笔记本电脑上,USB 硬盘被标记为不可移动。无论如何,这是我在不使用本机代码的情况下发现的最好的。
for (Path root : FileSystems.getDefault().getRootDirectories()) {
FileStore fileStore = Files.getFileStore(root);
System.out.format("%s\t%s\n", root, fileStore.getAttribute("volume:isRemovable"));
}
Run Code Online (Sandbox Code Playgroud)
来源:这个文件显然来自 JDK
你所问的没有定义。
我有一个拇指驱动器,当我插入它时,它会显示为 CD-ROM。当我在 CD-ROM 上运行程序后,它会附加第二个分区,该分区似乎是硬盘驱动器。
我有可移动 CD-ROM 驱动器。我的计算机外部也有“内部”eSATA 硬盘驱动器。
您必须非常努力才能获得驱动器“类型”的绑定定义。为什么你不告诉我们你想做什么,而不是询问你想要做什么?
| 归档时间: |
|
| 查看次数: |
29487 次 |
| 最近记录: |