Open In App

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

Last Updated : 13 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • path – the path of the file whose content type is to be guessed

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

Exception:

  • IOException – if an I/O error occurs
  • SecurityException – If the security manager denies access to the given file.

Java




// 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

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads