How to Remove Extension from Filename in Java
The following function removes extension from a filename using Java. However it will not modify the hidden file names in mac/linux which starts with a dot character.
A different implementation is available as part of the Apache commons IO library.
// Java example: removes extension from a file name.
public class ExtensionRemover {
public static void main(String[] args) {
String fileName = "a.hello.jpg";
System.out.println(removeExtension(fileName));
}
// Remove extension from file name
// Ignore file names starting with .
public static String removeExtension(String fileName) {
if (fileName.indexOf(".") > 0) {
return fileName.substring(0, fileName.lastIndexOf("."));
} else {
return fileName;
}
}
}