在Java 7中增加了新的一個方法——probeContentType,其主要作用是可以判斷文件的content type。相應代碼如下所示:
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileContentType { public static void main(String[] args) { printContentType("D:/Downloads/java.txt"); printContentType("D:/Downloads/java.ppt"); printContentType("D:/Downloads/java.doc"); printContentType("D:/Downloads/java.avi"); } private static void printContentType(String pathToFile) { Path path = Paths.get(pathToFile); String contentType = null; try { contentType = Files.probeContentType(path); } catch (IOException e) { e.printStackTrace(); } System.out.println("File content type is : " + contentType); } }
查看本欄目