如何获取所有驱动器的列表,但也获得相应的驱动器类型(可移动,本地磁盘或CD-ROM,DVD-ROM等)?

如何获取列表所有驱动器,但也获得相应的驱动器类型(可移动,本地磁盘,或CD-ROM,DVD-ROM等)?

使用此代码,您可以获取所有驱动器及其类型说明

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)); } 

假设它是windows,使用File.listRoots()来获取所有根。 然后使用FileSystemView检查它是软盘还是驱动器。 除此之外,我不知道。

你所要求的没有定义。

我有一个拇指驱动器,当我插入它时,显示为CD-ROM。 我在CD-ROM上运行程序后,它附加了第二个分区,它似乎是一个硬盘驱动器。

我有可移动的CD-ROM驱动器。 我的电脑外部还有“内置”eSATA硬盘。

你必须非常努力地获得驱动器“类型”的绑定定义。 你为什么不告诉我们你要做什么,而不是询问你想要做的具体方式?

如果使用JACOB ,则可以列出所有驱动器以及适当的类型:

 import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.EnumVariant; import com.jacob.com.JacobObject; import com.jacob.com.Variant; import java.io.File; import java.util.ArrayList; import java.util.List; public class DrivesExample { public interface HasNativeValue { int getNativeValue(); } public enum DriveTypeEnum implements HasNativeValue { Unknown(0), NoRootDirectory(1), RemovableDisk(2), LocalDisk(3), NetworkDrive(4), CompactDisc(5), RAMDisk(6); public final int nativeValue; DriveTypeEnum(int nativeValue) { this.nativeValue = nativeValue; } public int getNativeValue() { return nativeValue; } } private static  & HasNativeValue> T fromNative(Class clazz, int value) { for (T c : clazz.getEnumConstants()) { if (c.getNativeValue() == value) { return c; } } return null; } /** * The drive information. */ public static final class Drive { /** * File system on the logical disk. Example: NTFS. null if not known. */ public final String fileSystem; /** * Value that corresponds to the type of disk drive this logical disk represents. */ public final DriveTypeEnum driveType; /** * The Java file, eg "C:\". Never null. */ public final File file; public Drive(String fileSystem, DriveTypeEnum driveType, File file) { this.fileSystem = fileSystem; this.driveType = driveType; this.file = file; } @Override public String toString() { return "Drive{" + file + ": " + driveType + ", fileSystem=" + fileSystem + "}"; } } /** * Lists all available Windows drives without actually touching them. This call should not block on cd-roms, floppies, network drives etc. * * @return a list of drives, never null, may be empty. */ public static List getDrives() { List result = new ArrayList<>(); ActiveXComponent axWMI = new ActiveXComponent("winmgmts://"); try { Variant devices = axWMI.invoke("ExecQuery", new Variant("Select DeviceID,DriveType,FileSystem from Win32_LogicalDisk")); EnumVariant deviceList = new EnumVariant(devices.toDispatch()); while (deviceList.hasMoreElements()) { Dispatch item = deviceList.nextElement().toDispatch(); String drive = Dispatch.call(item, "DeviceID").toString().toUpperCase(); File file = new File(drive + "/"); DriveTypeEnum driveType = fromNative(DriveTypeEnum.class, Dispatch.call(item, "DriveType").getInt()); String fileSystem = Dispatch.call(item, "FileSystem").toString(); result.add(new Drive(fileSystem, driveType, file)); } return result; } finally { closeQuietly(axWMI); } } private static void closeQuietly(JacobObject jacobObject) { try { jacobObject.safeRelease(); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] arguments) { List drives = getDrives(); for (Drive drive : drives) { System.out.println(drive.toString()); } } } 

输出示例:

 Drive{C:\: LocalDisk, fileSystem=NTFS} Drive{D:\: LocalDisk, fileSystem=NTFS} Drive{E:\: RemovableDisk, fileSystem=NTFS} Drive{F:\: RemovableDisk, fileSystem=FAT32} Drive{G:\: RemovableDisk, fileSystem=null} Drive{Y:\: NetworkDrive, fileSystem=NTFS} 

要使用JACOB ,请将下载中的JARDLL添加为项目中的库。 这个解决方案当然只是Windows

此代码段适用于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")); } 

来源: 这个文件显然来自JDK

正确的方法是Luciano如何回答isRemovable属性,但我最近获胜10,如果我从USB开始,那么在该方法中不再找到USB。

因此,我仅在Windows中使用以下wmic调用:

 ArrayList rootDirs = new ArrayList(); if( isWin ) { if( i_dolog ) LOG.info( "Windows found as OS." ); ArrayList partids = new ArrayList(); String results0 = execute( "wmic diskdrive where interfacetype='USB' assoc /assocclass:Win32_DiskDriveToDiskPartition" ); String lines[] = results0.split( "\r\r\n" ); for( String line : lines ) { String test = "Win32_DiskPartition.DeviceID="; int index = line.indexOf( test ); if( index >= 0 ) { String partid = line.substring( index + test.length() + 1 ); partid = partid.substring( 0, partid.indexOf( '"' ) ); partids.add( partid ); } } for( String partition : partids ) { String results2 = execute( "wmic partition where (DeviceID='" + partition + "') assoc /assocclass:Win32_LogicalDiskToPartition" ); String lines2[] = results2.split( "\r\r\n" ); for( String line : lines2 ) { String test = "Win32_LogicalDisk.DeviceID="; int index = line.indexOf( test ); if( index >= 0 ) { String partid = line.substring( index + test.length() + 1 ); partid = partid.substring( 0, partid.indexOf( '"' ) ); rootDirs.add( new File( partid + "\\" ) ); } } } } 
 File[] roots = File.listRoots(); for(int i=0;i