Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the File Type in java

Tags:

java

file-type

How can I check whether the file version is .gz or .bzip2. I searched in File Java docs but couldn' find any method. Can you please let me know?

My requirement is to show the file on the UI , if .txt doesn't exist then check if .gz exists and if that doesnt exist then check if .bzip2 file exists and hence I am looking to check the extension of the file. I am assuming that I need to be looking at the Type of file.

like image 621
Akshitha Avatar asked Aug 14 '14 01:08

Akshitha


1 Answers

You may use Files Utility of Guava , and use the method of Files.getFileExtension(String String fullName)

System.out.println(Files.getFileExtension("C:\\fileName.txt"));

The output is:

txt

The source code is pretty simple though,

public static String getFileExtension(String fullName) {
    checkNotNull(fullName);
    String fileName = new File(fullName).getName();
    int dotIndex = fileName.lastIndexOf('.');
    return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
}
like image 67
JaskeyLam Avatar answered Oct 14 '22 13:10

JaskeyLam