2012-05-04 5 views

答えて

6

いいえ、ビルドインしません。ただし、ハードドライブ全体で特定の拡張子(またはより良いMIMEタイプ)のファイルをスキャンするクローラを作成できます。

ディレクトリを反復処理するための簡単な方法のために、この答えをチェックアウト:https://stackoverflow.com/a/3154523/691606

Get the Mime Type from a File参照してください。

import javax.activation.MimetypesFileTypeMap; 
import java.io.File; 

class MimeForMedia { 

    public static void main(String[] args) { 
     String[] types = { 
      "png", "jpg", "gif", "tif", "bmp", // image 
      "snd", "wav", "mp3", "ogg", "au", // sound 
      "mov", "avi", "mpg", "flv" // video 
      }; 
     MimetypesFileTypeMap typeMap = new MimetypesFileTypeMap(); 
     for (String type : types) { 
      String name = "a." + type; 
      String bestType = typeMap.getContentType(name); 
      if (bestType.equals("application/octet-stream")) { 
       System.out.print("Using URLConnection: "); 
       try { 
        bestType = (new File(name)).toURI().toURL() 
         .openConnection().getContentType(); 
       } catch(Exception e) { 
        System.out.println(e.getMessage()); 
       } 
      } 
      System.out.println (type + " " + bestType); 
     } 
    } 
} 

ここには、Windows 7 PCの結果が掲載されています。

Using URLConnection: png image/png 
jpg image/jpeg 
gif image/gif 
tif image/tiff 
Using URLConnection: bmp content/unknown 
Using URLConnection: snd audio/basic 
wav audio/x-wav 
Using URLConnection: mp3 content/unknown 
Using URLConnection: ogg content/unknown 
au audio/basic 
mov video/quicktime 
avi video/x-msvideo 
mpg video/mpeg 
Using URLConnection: flv content/unknown 
Press any key to continue . . . 
+0

* "(またはさらに良いMIMEタイプ)" *良いアイデアは、どのようなJ2SEクラスがMIME /コンテンツタイプを提供していますか?私はそれをカバーする 'File'か' FileSystemView'で何も見ることができませんでした。 –

+1

ほとんどの場合、外部ライブラリが必要です。 いくつかの例についてはhttp://www.rgagnon.com/javadetails/java-0487.htmlを参照してください。 – RadicalRaid

+0

ありがとう、私はこの方法を試してみよう! – HAxxor

5

ファイルシステムのすべてのフォルダを繰り返し、必要な拡張子を持つファイルを探します(時間がある場合)。