Skip to content
Related Articles
Open in App
Not now

Related Articles Interface in Java

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 01 Dec, 2021
Improve Article
Save Article
Like Article is a Java Interface. FileNameMap interface is a part of the package. FileNameMap provides a mechanism to map between a file name and a MIME type string. We can use FileNameMap getContentTypeFor method to get the MIME type of the specified file.


public interface FileNameMap

Method of Interface

The FileNameMap Interface consists of only one method named as:

getContentTypeFor(String fileName) Method – It is a part of interface. As the method name suggests, getContentFor is used to get the MIME type in String format for a specific file.


String getContentTypeFor(String fileName)

Method Parameter: This method has only one parameter which is of String Type.

Method Return Type: It has a String return type and will return the MIME type for the file.

How to invoke getContentTypeFor method?

Step 1: It load fileNameMap from a data file using URLConnection.getFileNameMap() static method

FileNameMap fileNameMap = URLConnection.getFileNameMap();

Step 2: It invokes FileNameMap.getContentTypeFor(String fileName) method and pass on the fileName to get it’s MIME type 

String mimeType = fileNameMap.getContentFor(String nameOfTheFile);

Example: Below is the java program for a better understanding of the FileNameMap interface and then to get the MIME type of the file using FileNameMap getContentTypeFor() method.


// Java program to demsonstrate the
// working of the FileNameMap interface
class GFG {
    public static void main(String[] args)
        try {
            FileNameMap fileNameMap
                = URLConnection.getFileNameMap();
            // specify all the fileName whose
            // MIME type we need to know
            String firstFileName = "tmp.txt";
            String secondFileName = "profile.png";
            String thirdFileName = "gfg.mp4";
            // call getContentTypeFor() method for each
            // fileName to get it's MIME type , method will
            // return null if fileName is invalid
            String firstFileMimeType = fileNameMap.getContentTypeFor(firstFileName);
            String secondFileMimeType = fileNameMap.getContentTypeFor(secondFileName);
            String thirdFileMimeType = fileNameMap.getContentTypeFor(thirdFileName);
            // print all the MIME types
            System.out.println("Mime type of "
                               + firstFileName + " is "
                               + firstFileMimeType);
            System.out.println("Mime type of "
                               + secondFileName + " is "
                               + secondFileMimeType);
            System.out.println("Mime type of "
                               + thirdFileName + " is "
                               + thirdFileMimeType);
        catch (Exception e) {
            System.out.println("Some Exception "
                               + e.getMessage());


Mime type of tmp.txt is text/plain
Mime type of profile.png is image/png
Mime type of gfg.mp4 is video/mp4

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!