Java Utililty Methods File Extension Name Extract

List of utility methods to do File Extension Name Extract

Description

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

Method

StringextractFileExtension(String path)
Extract the file extension from the given URI path.
int end = path.indexOf('?');
if (end == -1) {
    end = path.indexOf('#');
    if (end == -1) {
        end = path.length();
int begin = path.lastIndexOf('/', end) + 1;
...
StringextractFileExtensionFromPath(String path)
Extracts file's extension in ".extension" format.
if (path != null) {
    if (path.isEmpty()) {
        throw new IllegalArgumentException("File path can't be empty");
} else {
    throw new NullPointerException("File path can't be null");
String lowercaseName = path.toLowerCase();
...
StringgetFileExtension(File f)
get File Extension
String fileName = f.getName();
String fileExtension = fileName;
int lastPos = fileName.lastIndexOf('.');
if (lastPos > 0 && lastPos < (fileName.length() - 1)) {
    fileExtension = fileName.substring(lastPos + 1).toLowerCase();
return fileExtension;
StringgetFileExtension(File file)
get File Extension
String filename = file.getName().trim().toLowerCase();
if (filename.length() == 0)
    return "";
if (filename.charAt(filename.length() - 1) == '.')
    filename = filename.substring(0, filename.length() - 1);
int lastPointIndex = filename.lastIndexOf('.');
if (lastPointIndex < 0)
    return "";
...
StringgetFileExtension(File file)
get File Extension
String path = file.getPath();
Matcher matcher = FileNamePattern.matcher(path);
if (matcher.find()) {
    return matcher.group(2);
return null;
StringgetFileExtension(File file)
get File Extension
return getFileExtension(file.getPath());
StringgetFileExtension(File file)
get File Extension
if (file == null) {
    return null;
return getFileExtension(file.getName());
StringgetFileExtension(File file)
get File Extension
String name = file.getName();
int idx = name.lastIndexOf('.');
if (!file.isDirectory() && idx != -1) {
    return name.substring(idx + 1, name.length()).toLowerCase();
return "";
StringgetFileExtension(File file)
returns a file's extension/type, excluding the extension separator (by default a period '.')
return getFileExtension(file.getName());
StringgetFileExtension(File file)
get File Extension
if (file != null && file.exists()) {
    return getFileExtension(file.getAbsolutePath());
} else if (file == null)
    throw new NullPointerException("File is null");
else
    throw new FileNotFoundException("File [" + file.getAbsolutePath() + "] does not seem to exist.");