Java Files probeContentType() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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";
}
CallWhat 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

Parameters

ParameterDescription
pathThe file to examine.

Return Value

Return ValueDescription
StringA MIME type, or null if unknown.

Compare with Similar Methods

Identifying files:

MethodHow it differs
readAllBytes()Check magic bytes yourself.
isRegularFile()File, folder, or something else?
getAttribute()Other metadata by name.

Java Examples

Example 1 — Probe a Text File and an HTML File

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);
    }
}

How It Works

  1. Empty files. Both files have no content, which shows that the extension is what is being judged.
  2. Null-safe helper. typeOf turns a missing answer into "unknown", so callers never have to check for null.
  3. 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);
    }
}

How It Works

  1. Made-up extension. .qqq is not registered anywhere, so the detectors return null.
  2. No extension. README has nothing after a dot, and name-based detectors cannot guess from that.
  3. 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);
    }
}

How It Works

  1. Ask the system first. probeContentType can use richer information such as the Windows registry.
  2. JDK table second. guessContentTypeFromName works the same on every system and knows common types like PNG and PDF.
  3. 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.

Next: Files.readAllBytes()

Read a whole file into a byte array.

readAllBytes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful