10

ファイルが配置されているドライブの種類を検出する Java のプラットフォームに依存しない方法はありますか? 基本的に、ハードディスクリムーバブルドライブ(USBスティックなど)、ネットワーク共有を区別することに興味があります。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

FileSystemViewSwingのクラスには、ドライブの種類の検出をサポートする機能がいくつかあり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

これを使用してこれを決定する方法を示す Gist を次に示しますnet use

コードの最も重要な部分:

    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 に答える