10

Java是否有一种独立于平台的方法来检测文件所在的驱动器类型?基本上我有兴趣区分:硬盘可移动驱动器(如 U 盘)和网络共享。JNI/JNA 解决方案不会有帮助。可以假设 Java 7。

4

4 回答 4

5

您可以使用 Java 执行 cmd :

fsutil fsinfo drivetype {drive letter}

结果会给你这样的东西:

C: - Fixed Drive
D: - CD-ROM Drive
E: - Removable Drive
P: - Remote/Network Drive
于 2013-03-26T14:43:55.480 回答
4

Swing 中的FileSystemView类有一些功能来支持检测驱动器的类型(cf isFloppyDrive, isComputerNode)。恐怕没有标准的方法来检测驱动器是否通过 USB 连接。

人为的,未经测试的例子:

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
....
JFileChooser fc = new JFileChooser();
FileSystemView fsv = fc.getFileSystemView();
if (fsv.isFloppyDrive(new File("A:"))) // is A: a floppy drive? 

在 JDK 7 中还有另一种选择。我没用过,但是FileStoreAPI有type方法。文档说:

此方法返回的字符串格式是高度特定于实现的。例如,它可以指示使用的格式或文件存储是本地的还是远程的。

显然使用它的方法是这样的:

import java.nio.*;
....
for (FileStore store: FileSystems.getDefault().getFileStores()) {
    System.out.printf("%s: %s%n", store.name(), store.type());
} 
于 2012-02-06T16:50:39.127 回答
1

这是一个要点,它显示了如何使用以下方法确定这一点net usehttps ://gist.github.com/digulla/31eed31c7ead29ffc7a30aaf87131def

代码中最重要的部分:

    public boolean isDangerous(File file) {
        if (!IS_WINDOWS) {
            return false;
        }

        // Make sure the file is absolute
        file = file.getAbsoluteFile();
        String path = file.getPath();
//        System.out.println("Checking [" + path + "]");

        // UNC paths are dangerous
        if (path.startsWith("//")
            || path.startsWith("\\\\")) {
            // We might want to check for \\localhost or \\127.0.0.1 which would be OK, too
            return true;
        }

        String driveLetter = path.substring(0, 1);
        String colon = path.substring(1, 2);
        if (!":".equals(colon)) {
            throw new IllegalArgumentException("Expected 'X:': " + path);
        }

        return isNetworkDrive(driveLetter);
    }

    /** Use the command <code>net</code> to determine what this drive is.
     * <code>net use</code> will return an error for anything which isn't a share.
     * 
     *  <p>Another option would be <code>fsinfo</code> but my gut feeling is that
     *  <code>net</code> should be available and on the path on every installation
     *  of Windows.
     */
    private boolean isNetworkDrive(String driveLetter) {
        List<String> cmd = Arrays.asList("cmd", "/c", "net", "use", driveLetter + ":");

        try {
            Process p = new ProcessBuilder(cmd)
                .redirectErrorStream(true)
                .start();

            p.getOutputStream().close();

            StringBuilder consoleOutput = new StringBuilder();

            String line;
            try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
                while ((line = in.readLine()) != null) {
                    consoleOutput.append(line).append("\r\n");
                }
            }

            int rc = p.waitFor();
//            System.out.println(consoleOutput);
//            System.out.println("rc=" + rc);
            return rc == 0;
        } catch(Exception e) {
            throw new IllegalStateException("Unable to run 'net use' on " + driveLetter, e);
        }
    }
于 2016-11-18T08:34:03.900 回答
0

看看这个讨论:如何获取所有驱动器的列表,同时也获取相应的驱动器类型(可移动、本地磁盘或 cd-rom、dvd-rom 等)?

具体关注http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/filechooser/FileSystemView.html

我个人没有使用过这个,但它似乎相关。它有类似 is 的方法isFloppyDrive

也看看JSmooth

于 2012-02-06T16:57:44.833 回答