In Java, Files.probeContentType(path) returns a guess at the file's MIME type as a string such as "text/plain" or "image/png", or null if it cannot tell.
It first asks any installed FileTypeDetector providers, then a platform default: the Windows registry, the macOS type system, or MIME databases on Linux. Most of these look only at the file name extension, so results can differ between systems and JDK versions.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.probeContentType(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Pass a path and always handle null:
Java
String type = Files.probeContentType(path);
if (type == null) {
type = "application/octet-stream";
}
Call
What it does
"notes.txt"
Typically text/plain.
"photo.png"
Typically image/png.
"data.qqq"
Typically null.
Since Java 7. The file usually does not even need to exist, because most detectors only read the name.
Library declarations
Java
public static String probeContentType(Path path) throws IOException
A small helper replaces null with "unknown" so the output never prints the word null.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ProbeContentTypeBasic {
static String typeOf(Path p) throws IOException {
String type = Files.probeContentType(p);
return type == null ? "unknown" : type;
}
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path txt = Files.write(dir.resolve("notes.txt"), new byte[0]);
Path html = Files.write(dir.resolve("page.html"), new byte[0]);
System.out.println("notes.txt: " + typeOf(txt));
System.out.println("page.html: " + typeOf(html));
Files.delete(txt);
Files.delete(html);
Files.delete(dir);
}
}
Output
Run with java ProbeContentTypeBasic. Typical output on current JDKs for Windows, macOS, and Linux; some systems (for example JDK 8 on macOS) print unknown.
notes.txt: text/plain
page.html: text/html
How It Works
Empty files. Both files have no content, which shows that the extension is what is being judged.
Null-safe helper.typeOf turns a missing answer into "unknown", so callers never have to check for null.
Well-known types. .txt and .html are registered on practically every system, so these are the usual answers.
Example 2 — Unknown Extensions Fall Back to a Default
For names the system does not recognise, use the generic binary type.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ProbeContentTypeUnknown {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
String[] names = {"archive.qqq", "README"};
for (String name : names) {
Path p = Files.write(dir.resolve(name), new byte[] {1, 2, 3});
String type = Files.probeContentType(p);
System.out.println(name + " -> " + (type != null ? type : "application/octet-stream (fallback)"));
Files.delete(p);
}
Files.delete(dir);
}
}
Output
Run with java ProbeContentTypeUnknown. Typical output on Windows, macOS, and Linux; a system with a custom MIME database or a content-sniffing detector could recognise these files.
Made-up extension. .qqq is not registered anywhere, so the detectors return null.
No extension. README has nothing after a dot, and name-based detectors cannot guess from that.
Safe default. application/octet-stream means "some binary data", the standard type for downloads of unknown kind.
Example 3 — Fall Back to URLConnection's Built-In Table
If probing returns null, ask URLConnection.guessContentTypeFromName, which uses a table shipped with the JDK.
Java
import java.io.IOException;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
public class ProbeContentTypeFallback {
static String mimeOf(Path p) throws IOException {
String type = Files.probeContentType(p);
if (type == null) {
type = URLConnection.guessContentTypeFromName(p.getFileName().toString());
}
return type == null ? "application/octet-stream" : type;
}
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
for (String name : new String[] {"photo.png", "report.pdf"}) {
Path p = Files.write(dir.resolve(name), new byte[0]);
System.out.println(name + ": " + mimeOf(p));
Files.delete(p);
}
Files.delete(dir);
}
}
Output
Run with java ProbeContentTypeFallback. Typical output on Windows, macOS, and Linux; the fallback chain makes these two results very likely everywhere.
photo.png: image/png
report.pdf: application/pdf
How It Works
Ask the system first.probeContentType can use richer information such as the Windows registry.
JDK table second.guessContentTypeFromName works the same on every system and knows common types like PNG and PDF.
Last resort. If both fail, the helper returns the generic binary type, so the method never returns null.
Frequently Asked Questions
Files.probeContentType(path), and handle a null result with a fallback such as application/octet-stream.
No detector recognised the file, which is common for unusual extensions and on some JDK and OS combinations.
Usually not; the default detectors mostly use the file name extension. Custom FileTypeDetector providers may read content.
Write a java.nio.file.spi.FileTypeDetector subclass and register it in META-INF/services/java.nio.file.spi.FileTypeDetector.
No. Anyone can rename a file. Check the actual bytes, for example a PNG signature, when it matters.