Open In App

java.nio.file.spi.FileTypeDetector Class in Java

java.nio.file.spi.FileTypeDetector Class extends java.lang.Object Class. FileTypeDetector Class contain method to know about the content type of given file.

Class declaration:



public abstract class FileTypeDetector
extends Object

Constructor:

Constructor Description
protected FileTypeDetector() It is used the creation of a new object of FileTypeDetector class. 

Method:



Method Description
probeContentType(Path path) It is used to guess the content type of the given file. 

abstract String probeContentType(Path path): It is used to guess the content type of the given file. To know the content type of the file this method may examine the filename, use file attribute, or even examine the bytes in the file. The way of examination of file solely depends on the implementation.

Parameters:

Returns: The content type of the given file.If the file type is not recognized it returns null.

Exception:




// Java program to illustrate use of probeContentType()
// method of FileTypeDetector class
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class GFG {
   
    public static void main(String[] args)
    {
 
        try {
           
            // create object of Path
            Path path = (Path)Paths.get("/usr", "local",
                                        "bin", "file.txt");
            // Print content type of the file present at
            // this path
            System.out.println(
                Files.probeContentType(path));
        }
        catch (IOException e) {
 
            e.printStackTrace();
        }
    }
}

 
 

Output:

 

text/plain

 

Article Tags :