Java - File MIME Type

Introduction

Files.probeContentType(Path path) method probes the content type of a file.

The method returns the content type as Multipurpose Internet Mail Extension (MIME) content type.

If the content type of a file cannot be determined, it returns null.

The following code shows how to probe the content type of a file.

Demo

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path p = Paths.get("C:\\myData\\Main.java");

    try {/*  w ww . ja  va 2 s. c  om*/
      String contentType = Files.probeContentType(p);
      System.out.format("Content type of %s is %s%n", p, contentType);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Result