How can I get list of all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom… etc)?

前端 未结 7 1035
猫巷女王i
猫巷女王i 2020-12-01 14:42

How can I get list all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom... etc)?

相关标签:
7条回答
  • 2020-12-01 14:46

    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.

    0 讨论(0)
  • 2020-12-01 14:53

    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 + "\\" ) );
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-01 15:00

    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));
    }
    
    0 讨论(0)
  • 2020-12-01 15:01
    File[] roots = File.listRoots();
    for(int i=0;i<roots.length;i++)
        System.out.println("Root["+i+"]:" + roots[i]);
    
    0 讨论(0)
  • 2020-12-01 15:06

    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

    0 讨论(0)
  • 2020-12-01 15:07

    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?

    0 讨论(0)
提交回复
热议问题