Pin It

Java 7 new feature : Determining the file content type

Program to demonstrate Java 7 new feature : Determining the file content type.

package com.hubberspot.nio;

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

public class FileContentType {

    public static void main(String[] args) {
        
        printContentType("D:/Downloads/java.txt");
        printContentType("D:/Downloads/java.ppt");
        printContentType("D:/Downloads/java.doc");
        printContentType("D:/Downloads/java.jar");

    }

    private static void printContentType(String pathToFile) {
        
        Path path = Paths.get(pathToFile);
        String contentType = null;
        try {
            contentType = Files.probeContentType(path);
        } catch (IOException e) {
     
            e.printStackTrace();
        }
        System.out.println("File content type is : " + contentType);
        
    }

}



Output of the program : 

 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot