how to get the extension of a file in java code example
Example 1: java how to read file extension
private static String getFileExtension(File file) {
String extension = "";
try {
if (file != null && file.exists()) {
String name = file.getName();
extension = name.substring(name.lastIndexOf("."));
}
} catch (Exception e) {
extension = "";
}
return extension;
}
Example 2: how to get information from any file extension in java
import java.util.LinkedList;
import java.io.File;
public class getFileExtention{
public static String[] getExt(File path, String extentionType) {
String [] g = path.list();
LinkedList<String> h = new LinkedList<String>();
for(int c = 0;c < g.length;c++) {
if(g[c].endsWith(extentionType)) {
h.add(g[c]);
}
}
String[] j = new String[h.size()];
for (int d = 0; d < h.size(); d++) {
j[d] = h.get(d);
}
return j;
}
}