Get file extension name in Java
Description
The following code shows how to get file extension name.
Example
/*from w ww. j av a 2s .co m*/
import java.io.File;
public class Main {
public static void main(String[] argv) {
getFileExtensionName(new File("a.txt"));
}
public static String getFileExtensionName(File f) {
if (f.getName().indexOf(".") == -1) {
return "";
} else {
return f.getName().substring(f.getName().length() - 3, f.getName().length());
}
}
}