首页 > 解决方案 > 在 MacOS 上列出外部驱动器时出现 java 错误

问题描述

我正在尝试列出计算机中的所有驱动器,包括附加的驱动器。

            FileStore fileStore = null;
            try {
                fileStore = Files.getFileStore(root);
                System.out.format("%s\t%s\n", root, fileStore.getAttribute("volume:isRemovable"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }```

I tried the above code but I am receiving the following error when I have an external USB drive attached:

java.lang.UnsupportedOperationException: 'volume:isRemovable' not recognized

    at sun.nio.fs.UnixFileStore.getAttribute(UnixFileStore.java:150)
    at com.roscasend.javacore.utils.FileUtilities.listComputerDrives(FileUtilities.java:27)

Best Regards,
Aurelian

标签: javanio

解决方案


我找到了以下答案,它帮助我显示了来自 mac os 计算机的所有驱动器:

    File [] osxVols = new File("/Volumes").listFiles();
    for(int i=0; i<osxVols.length; i++) {
        map.put(osxVols[i].getAbsolutePath(), osxVols[i].getName());
    }```

推荐阅读