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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With