Java Utililty Methods File Name Extract

List of utility methods to do File Name Extract

Description

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

Method

StringextractFileName(String path)
extract File Name
int index = path.lastIndexOf("\\");
if (index > -1) {
    if (index < path.length() - 1) {
        return path.substring(index + 1);
    } else {
        return "";
index = path.lastIndexOf("/");
if (index > -1) {
    if (index < path.length() - 1) {
        return path.substring(index + 1);
    } else {
        return "";
return path;
StringextractFileName(String path)
Pass in /sub/dir/path.html returns path.html
if (path == null) {
    return null;
String newpath = path.replace('\\', '/');
int start = newpath.lastIndexOf("/");
if (start == -1) {
    start = 0;
} else {
...
StringextractFileName(String path)
Extracts filename portion out of supplied pathname (leaves last segment only)
if (path == null || path.isEmpty())
    return path;
int pos = path.lastIndexOf('/');
if (pos < 0)
    pos = path.lastIndexOf('\\');
if (pos >= 0)
    return path.substring(pos + 1);
return path;
...
StringextractFilename(String path)
Extract the filename from a (relative or absolute) path.
String result = path.substring(path.lastIndexOf('/') + 1);
return result.substring(result.lastIndexOf('\\') + 1);
StringextractFileName(String url)
Extract the file name from the URL removing the scheme, domain, query params and named anchor, if present.
int index1 = url.indexOf('?');
int index2 = url.indexOf('#');
if (index1 == -1) {
    index1 = url.length() + 1;
if (index2 == -1) {
    index2 = url.length() + 1;
int index = Math.min(index1, index2);
if (index < url.length()) {
    url = url.substring(0, index);
index1 = url.lastIndexOf('/');
index2 = url.lastIndexOf('\\');
index = Math.max(index1, index2);
url = url.substring(index + 1);
return url;
StringextractFileNameFromBAMLocation(String location)
Converts strings like /data/results/DNA1234049.bam or DNA1234049.bam to DNA1234049
String fileName = "";
location = location.toLowerCase();
String[] split = location.split("/");
for (String s : split) {
    if (s.endsWith(".bam")) {
        fileName = s.replaceAll(".bam$", "");
return fileName;
StringextractFileNameFromContentDisposition(String contentDisposition)
Extract the file name from the content disposition header.
System.out.println("content disposition = " + contentDisposition);
String[] attributes = contentDisposition.split(";");
for (String a : attributes) {
    if (a.toLowerCase().contains("filename")) {
        try {
            return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"'));
        } catch (Exception e) {
            return a.substring(a.indexOf('=') + 1, a.length());
...
StringextractFileNameFromPath(final String filePath, char separatorChar)
extract File Name From Path
int index = filePath.lastIndexOf(separatorChar);
if (index < 0) {
    return filePath;
return filePath.substring(index + 1);
StringextractFileNameFromPath(final String path)
Returns the file name from the path Never null
final int lastIndex = path.lastIndexOf('/');
if (lastIndex == -1) {
    throw new IllegalArgumentException("The specified path '" + path + "' doesn't contain '/'");
} else {
    return path.substring(lastIndex + 1, path.length());
StringextractFilenameFromPath(String path)
Returns only file name from path
String sTmp = path.replaceAll("\\\\", "/");
int lastPos = sTmp.lastIndexOf('/');
if (lastPos >= 0)
    return sTmp.substring(lastPos + 1);
else
    return sTmp;