Files.probeContentType() for MIME types
The NIO.2 Files class includes a method to guess the MIME type (content type) of a file based on its extension or content.
How it Works
Files.probeContentType(path) uses the installed FileTypeDetector implementations of the platform to determine the type (e.g., "text/plain", "image/jpeg").
Java Example
import java.nio.file.*;
import java.io.IOException;
public class ContentTypeExample {
public static void main(String[] args) throws IOException {
Path path = Paths.get("test.html");
String type = Files.probeContentType(path);
System.out.println("Content Type: " + type);
}
}
Output
Content Type: text/html (May vary by system)
Key Points
- Detect file MIME types
- Uses platform-specific detectors
- Useful for web applications
- Supports custom detectors
Comments