Library contents

Java Files.probeContentType

Ask installed file-type detectors for a suggested media type.

A simple example

Files.probeContentType

  1. Start with Filename a.txt
  2. Do this Ask for a type hint
  3. Result Often text/plain
The extension suggests plain text. Detection depends on the platform and its type database.

Syntax

Java syntax
public static String probeContentType(Path path) throws IOException
JDK 21 declarations (1)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Choose a fallback for an attachment preview

A support team receives an attachment with a new extension that the desktop may not recognize. Its preview needs a usable media type even on machines where the detector returns no answer.

Resulttrue
Main.java
import java.nio.file.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path note = Files.writeString(desk.resolve("notes.workshopdata"), "notes");
            String guess = Files.probeContentType(note);
            String previewType = guess == null ? "application/octet-stream" : guess;
            System.out.println(!previewType.isBlank());
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
true

Either a detector supplies the suggestion or the preview uses a general binary fallback.

Advertisements

Choose a viewer for a plain-text handout

A teacher distributes a short handout with a text extension. Ask the local detector which media type to try, and use a download-only action when a system has no matching text detector.

ResultOpen text viewer
Main.java
import java.nio.file.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path handout = Files.writeString(desk.resolve("handout.txt"), "Read chapter one.");
            String type = Files.probeContentType(handout);
            System.out.println("text/plain".equals(type) ? "Open text viewer" : "Offer download");
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
Open text viewer

On the tested machine the detector recognizes text/plain. A provider returning null or another type takes the download branch.

Choose a reader for a text note

A student sends a plain text file to a preview kiosk. The preview desk checks the usual MIME type associated with this extension on its configured system. Which media type is expected in this exercise?

Givencrib.txt · standard MIME mapping
Worked resulttext/plain

What to watch for

The answer depends on the system and can be null. A filename-based guess cannot establish the actual format of untrusted content.

Documentation for Files.probeContentType