VOOZH about

URL: https://www.geeksforgeeks.org/java/java-net-filenamemap-interface-in-java/

⇱ java.net.FileNameMap Interface in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

java.net.FileNameMap Interface in Java

Last Updated : 23 Jul, 2025

java.net.FileNameMap is a Java Interface. FileNameMap interface is a part of the java.net 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.

Syntax:

public interface FileNameMap

Method of java.net.FileNameMap Interface

The FileNameMap Interface consists of only one method named as:

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

Syntax:

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.


Output
Mime type of tmp.txt is text/plain
Mime type of profile.png is image/png
Mime type of gfg.mp4 is video/mp4
Comment
Article Tags: