2016-02-12 173 views
2

我是Java新手,正在嘗試學習如何確定文件的MIME類型。我正在使用Mac OS。以下是我提出的代碼。但是,當我運行代碼時,IDE輸出錯誤:無法確定文件的MIME類型

'/Users/justin/Desktop/Codes Netbean/JavaRandom/xanadu.txt' has an unknown filetype. 

爲什麼會發生這種情況?該文件確實存在。難道我做錯了什麼?

public class DeterminingMIMEType { 

    public static void main(String[] args) { 

     Path filename = Paths.get("/Users/justin/Desktop/Codes Netbean/JavaRandom/xanadu.txt"); 

     try { 
      String type = Files.probeContentType(filename); 
      if (type == null) { 
       System.err.format("'%s' has an" + " unknown filetype.%n", filename); 
      } else if (!type.equals("text/plain")) { 
       System.err.format("'%s' is not" + " a plain text file.%n", filename); 
      } 
     } catch (IOException x) { 
      System.err.println(x); 
     } 
    } 
} 

回答