Java IO & NIO - Files.probeContentType() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static String probeContentType(Path path)
                               throws IOException

This method uses the installed FileTypeDetector implementations to probe the given file to determine its content type.

Parameters:
path - the path to the file to probe
Returns:
The content type of the file, or null if the content type cannot be determined


Examples


package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ProbeContentTypeExample {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test-file", ".txt");
System.out.println("Probe Content Type for file: " + path);
String type = Files.probeContentType(path);
System.out.println("File Content type: " + type);

path = Files.createTempFile("test-file", ".html");
System.out.println("Probe Content Type for file: " + path);
type = Files.probeContentType(path);
System.out.println("File Content type: " + type);
}
}

Output

Probe Content Type for file: C:\Users\Joe\AppData\Local\Temp\test-file5673809405755370146.txt
File Content type: text/plain
Probe Content Type for file: C:\Users\Joe\AppData\Local\Temp\test-file14702966924326758655.html
File Content type: text/html




See Also