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 1055
猫巷女王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 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

提交回复
热议问题