2012-02-06 29 views
9

ファイルが配置されているドライブの種類を検出するためのJavaにプラットフォームに依存しない方法はありますか?基本的には、ハードディスクリムーバブルドライブ(USBスティックのような)とネットワーク共有の間で区別することに興味があります。 JNI/JNAソリューションは役に立ちません。 Java 7が想定されます。スウィングからJava:ファイルが配置されているドライブの種類を判別する方法は?

+0

おそらく、これを知りたい理由を解決する方が簡単かもしれません。 –

+2

パフォーマンス、ファイルシステムの監視がうまくいかないなど、基本的なファイルシステムの特定の欠点についてユーザーに警告するために必要な情報です。 – mstrap

+0

http://stackoverflow.com/questions/3542018/how-can-i-get-list-of-all-drives-but-also-get-the-corresponding-drive-type-remo/17972420#17972420を参照してください。 私はWMIを使用して必要な情報にアクセスしています。 –

答えて

3

FileSystemViewクラスは、ドライブ(CF isFloppyDriveisComputerNode)の種類を検出するサポートする一部機能を有しています。 USB経由でドライブが接続されているかどうかを検出する標準的な方法はありません。

不自然、テストされていない例:JDK 7で

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? 

別のオプションがあります。私はそれを使用していませんが、 APIはtypeメソッドを持っています。 documentationには次のように書かれています。

このメソッドから返される文字列の形式は、実装固有のものです。たとえば、使用されている形式や、ファイルストアがローカルまたはリモートの場合などです。

どうやらそれを使用する方法は、このようになります:

import java.nio.*; 
.... 
for (FileStore store: FileSystems.getDefault().getFileStores()) { 
    System.out.printf("%s: %s%n", store.name(), store.type()); 
} 
+4

FileSystemView.isFloppyDrive()は "path.equals(" A:\\ ")"のようなもので実装されていますが、残念なことに役立たないでしょう。また、FileStore.type()が役に立ちませんでした。私のネットワーク共有の "NTFS"を返します。 – mstrap

+3

FileStore#type()は、Windows 7、Java 7上のネットワークドライブの "NTFS"を返します。 –

+0

FileStore#type()は、Windows 10のローカルハードドライブについても "NTFS"を返します。 – simpleuser

1

がこの議論に見てみましょう:How can I get list of all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom... etc)?

は、具体的に、私は個人的にこれを使用していないhttp://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/filechooser/FileSystemView.html

に注意を払いますそれは関連しているようです。それはisFloppyDriveのようなメソッドを持っています。

はまた、あなたがしてJavaを使用してcmdを実行できJSmooth

+0

FileSystemViewは機能しません。私はJRE自体に付属しているAPIを見つけようとしていましたが、JSmoothが機能するかもしれません。 – mstrap

4

に見てみましょう:

fsutil fsinfo drivetype {drive letter} 

結果はあなたにこのような何か与える:ここでは

C: - Fixed Drive 
D: - CD-ROM Drive 
E: - Removable Drive 
P: - Remote/Network Drive 
+3

プラットフォームに依存せず、fsutilに管理者権限が必要なように見えます。 – mstrap

+1

このコードは非常に役に立ちました。http://stackoverflow.com/questions/10678363/find-the-directory-for-a-filestore – MAbraham1

+0

私も(非常に役に立ちました) – philwalk

0

はその要旨でありますnet useを使用してこれを判断する方法を示します。https://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); 
     } 
    } 
関連する問題