Removing the extension from a filename Let me know if the given code can be improved or optimized:

public static String getFileNameWithoutExtensiotn(File file) {
    String fileName = null;
    if (file.getName().startsWith(".")) {//if starts with "." like .htaccess
        fileName = file.getName();
    } else {
        try {
            fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
        } catch (Exception e) {
            fileName = file.getName();
        }
    }

    return fileName;
}
Your code can be improved in few lines:

        String fileName = file.getName();
        if (fileName.indexOf(".") &gt; 0)
            fileName = fileName.substring(0, fileName.lastIndexOf("."));
        return fileName;


As far as catching the exception, an advice, always catch the specific exception, not the generic, so you should be catching IOException or IndexOutOfBoundException instead of Exception. Also, it's always a good idea to log exception somehow.