Finding the Mime Type of a File

Finding the Mime Type of a File

Finding the MIME type of a file is useful when you want to perform certain actions.

PS: I have placed a file named nature.jpg in the folder /home/sridhar before executing this class.

Code sample:

import java.nio.file.*;import*;public class FindMIMEType{   public static void main(String args[])   {      FindMIMEType findMIMEType = new FindMIMEType();      findMIMEType.proceed();   }      private void proceed()   {      Path filename = Paths.get("/home/sridhar/nature.jpg");            try       {         //This method probeContentType returns the content type of the file being considered         String mimeType = Files.probeContentType(filename);         System.out.println("MIME type of " + filename + " is " + mimeType);      }       catch (IOException ioe)       {         System.out.println(ioe);      }         }   }/*

Expected output:

[root@mypc]# java FindMIMETypeMIME type of /home/sridhar/nature.jpg is image/jpeg*/

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist