Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 13 Apr, 2021

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:

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

Method:

MethodDescription
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

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :