How can I get list all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom... etc)?
Assuming it's windows, use File.listRoots()
to get all roots.
Then use FileSystemView to check whether it's floppy disk or a drive. Other than that I have no idea.
The correct way is how Luciano answered with isRemovable property, but I recently had on win 10 that if I started from USB, then USBs were no longer found in that method.
So, I used, for Windows only, the following wmic calls:
ArrayList<File> rootDirs = new ArrayList<File>();
if( isWin )
{
if( i_dolog )
LOG.info( "Windows found as OS." );
ArrayList<String> partids = new ArrayList<String>();
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 + "\\" ) );
}
}
}
}
With this code you can get all drives and its type description
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));
}
File[] roots = File.listRoots();
for(int i=0;i<roots.length;i++)
System.out.println("Root["+i+"]:" + roots[i]);
This code snippet works on Windows. It correctly reports USB pen drives as removable, but on my laptop a USB hard drive is marked as non removable. Anyway this is the best I found without using native code.
for (Path root : FileSystems.getDefault().getRootDirectories()) {
FileStore fileStore = Files.getFileStore(root);
System.out.format("%s\t%s\n", root, fileStore.getAttribute("volume:isRemovable"));
}
Source: this file that apparently comes from the JDK
There's no definition for what you're asking.
I have a thumb drive which, when I plug it in, shows up as a CD-ROM. After I run a program on the CD-ROM it attaches a second partition which appears to be a hard drive.
I have removable CD-ROM drives. I also have "internal" eSATA hard drives on the outside of my computer.
You'd have to work pretty hard to get a binding definition for the "type" of the drive. Why don't you tell us what you're trying to do, instead of asking about the particular way you want to do it?