Java Files get file content type

Description

Java Files get file content type

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

public class Main {

    public static void main(String[] args) throws Exception {
        displayContentType("/home/docs/users.txt");
        displayContentType("/home/docs/Chapter 2.doc");
        displayContentType("/home/docs/java.exe");
    }//from   ww w  .ja va 2s. c  om

    static void displayContentType(String pathText) throws Exception {
        Path path = Paths.get(pathText);
        String type = Files.probeContentType(path);
        System.out.println(type);
    }

}
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("Main.java");

    try {/*from w w w . j a v a 2s .  com*/
      String contentType = Files.probeContentType(p);
      System.out.println(contentType);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related