Java Utililty Methods File Name Get

List of utility methods to do File Name Get

Description

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

Method

StringgetFileName(String dir, String name)
get File Name
String res = dir;
if (res.charAt(res.length() - 1) == '/') {
    res = res.substring(0, res.length() - 1);
char[] mm = { File.separatorChar };
String sp = String.copyValueOf(mm);
StringTokenizer st = new StringTokenizer(name, "/");
while (st.hasMoreElements()) {
...
StringgetFileName(String f)
This method returns the file name of a given file-path which is passed as parameter.
String fn = null;
int i = f.lastIndexOf(String.valueOf(File.separatorChar));
if (i != -1) {
    int j = f.lastIndexOf(".");
    if (j != -1) {
        try {
            fn = f.substring(i + 1, j);
        } catch (IndexOutOfBoundsException ex) {
...
StringgetFileName(String fileName)
get File Name
if (isEmpty(fileName)) {
    return null;
return fileName.substring(0, fileName.lastIndexOf("."));
StringgetFileName(String filePath)
An OS independent getName alternative.
String tempFileName = filePath;
int slash1 = tempFileName.lastIndexOf("/");
int slash2 = tempFileName.lastIndexOf("\\");
int lastSlashIndex = Math.max(slash1, slash2);
if (lastSlashIndex != -1) {
    tempFileName = tempFileName.substring(lastSlashIndex + 1);
return tempFileName;
...
StringgetFileName(String filePath)
get the file displayName from a file path;
if (filePath == null) {
    return null;
} else {
    int index = filePath.lastIndexOf('\\');
    if (index >= 0) {
        return filePath.substring(index + 1);
    } else {
        index = filePath.lastIndexOf('/');
...
StringgetFileName(String filepath)
get File Name
if (filepath == null) {
    return null;
File f = toFile(filepath);
return f.getName();
StringgetFileName(String filePath)
get File Name
File f = new File(filePath);
if (f.exists()) {
    return f.getName();
} else {
    return null;
StringgetFileName(String filePath)
Returns only the fileName of the filePath.
int lastIndex = filePath.lastIndexOf(File.separator);
String result = filePath.substring(lastIndex + 1, filePath.length());
return result;
StringgetFilename(String filePath)
get Filename
File f = new File(filePath);
return f.getName();
StringgetFileName(String filePath)
The file name of the specified file path is returned.
File file = new File(filePath);
String fileName = file.getName();
int postion = fileName.lastIndexOf('.');
if (postion == -1)
    return fileName;
return fileName.substring(0, postion);