Java Utililty Methods File Extension Name Get

List of utility methods to do File Extension Name Get

Description

The list of methods to do File Extension Name Get are organized into topic(s).

Method

StringgetFileExtension(File file, boolean withDot)
Returns extension of specified file.
if (file == null) {
    return null;
String path = file.getAbsolutePath();
int dotAt = path.lastIndexOf('.');
if (dotAt < 0) {
    return null;
if (!withDot) {
    dotAt++;
return path.substring(dotAt);
StringgetFileExtension(final File aFile)
Returns the "presumed" filename extension (like '.jpg', '.zip') from a given file.
String ext = "";
String filename = aFile.getName();
int idx = filename.lastIndexOf('.');
if ((idx >= 0) && (idx < (filename.length() - 1))) {
    ext = filename.substring(idx + 1).toLowerCase();
if (aFile.exists() && aFile.isDirectory() && !"".equals(ext)) {
    ext = "";
...
StringgetFileExtension(final File file)
Gets the extension of a filename.
if (file == null) {
    throw new IllegalArgumentException("Unable to get the file extension from 'null'");
final String fileName = file.getName();
final int lastIndexOfDot = fileName.lastIndexOf('.');
if (lastIndexOfDot > 0) {
    return fileName.substring(lastIndexOfDot + 1);
return "";
StringgetFileExtension(final File file)
get File Extension
final String ext;
if (file != null) {
    final String scriptFileName = file.getName();
    ext = scriptFileName.lastIndexOf('.') == -1 ? ""
            : scriptFileName.substring(scriptFileName.lastIndexOf('.') + 1, scriptFileName.length());
} else {
    ext = "";
return ext;
StringgetFileExtension(final String file)
Returns the file extension of a file
final int lastIndexOf = file.lastIndexOf('.');
if (lastIndexOf == -1) {
    return ""; 
return file.substring(lastIndexOf + 1);
StringgetFileExtension(final String fullName)
Returns the file extension for the given file name, or the empty string if the file has no extension.
Assert.isNotNull(fullName, "fullName must be given!"); 
final String fileName = new File(fullName).getName();
final int dotIndex = fileName.lastIndexOf('.');
return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1); 
StringgetFileExtension(String file)
get File Extension
return getFileExtension(new File(file));
StringgetFileExtension(String filename)
get File Extension
if (filename != null) {
    int n = filename.lastIndexOf('.');
    if (n >= 0) {
        return filename.substring(n + 1);
return null;
StringgetFileExtension(String fileName)
get File Extension
int i = fileName.lastIndexOf('.');
int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
if (i > p) {
    return fileName.substring(i + 1);
return "";
StringgetFileExtension(String filename)
exclude "."
try {
    return filename.substring(filename.lastIndexOf(".") + 1);
} catch (Exception e) {
    return null;