if no expanded-name, how to get file type(image/audio or video) by java?
I want to write a function like this:
String getFileType(String filePath){
// TODO:...
return type;
}
if no expanded-name, how to get file type(image/audio or video) by java?
I want to write a function like this:
String getFileType(String filePath){
// TODO:...
return type;
}
Use the following code to get the extension of the file:-
public String getFileType(String fileName) {
String extension = "";
String extensionNew = "";
int index = fileName.lastIndexOf(".");
if (index > 0) {
extension = fileName.substring(index + 1);
extensionNew = fileName.substring(index);
}
System.out.println("File extension is: " + extension);
System.out.println("File extension new is: " + extensionNew);
return extensionNew;
}
Otherwise, Apache Common IO is very popular API for file manipulations. Use the following link to download the jar. http://www.java2s.com/Code/Jar/c/Downloadcommonsio24jar.htm Download those jars, and finish it simple as follows:-
import org.apache.commons.io.FilenameUtils;
public class FileExtensionNew {
public static void main(String[] args) {
String extension = "";
String extension2 = "";
try {
extension = FilenameUtils.getExtension("Hello.java"); // return ---> "java"
extension2 = FilenameUtils.getExtension("home/java/Test.jar"); // return ---> "jar"
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("File extension is: " + extension);
System.out.println("File extension is: " + extension2);
}
}
Hope, it will help you. Thanks!