Reputation: 10038
I'm new to Java and is trying to learn how to determine the MIME type of a file. I'm using Mac OS. Below is the code I came up with. However, when I run the code, the IDE output error:
'/Users/justin/Desktop/Codes Netbean/JavaRandom/xanadu.txt' has an unknown filetype.
Why is this happening? The file does exist. Am I doing something wrong?
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);
}
}
}
Upvotes: 2
Views: 2546
Reputation: 148
The documentation for Files reveals that a FileTypeDetector is loaded by ServiceLoader. A wee bit of googling leads to: http://blog.byjean.eu/java/2013/08/22/making-jdk7-nio-filetypedetection-work-on-mac-osx.html which says that this is a problem with the default FileTypeDetector provided by the Oracle Java7 jvm for Mac OS. The link also has a way of providing your own FileTypeDetector, though upgrading to Java 8 (maybe?) also will fix the problem.
Upvotes: 2