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

StringgetFileNameWithoutExtension(File file)
Get the short name portion of a filename not including the extension.
String fileName = file.getName();
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
    return fileName.substring(0, i);
} else {
    return fileName;
StringgetFileNameWithoutExtension(File fx)
get File Name Without Extension
String path = null;
try {
    path = fx.getCanonicalPath();
} catch (IOException e1) {
    path = fx.getAbsolutePath();
return getFileNameWithoutExtension(path);
StringgetFilenameWithoutExtension(final File file)
Gets the filename without the extension or null if the given file object is a directory.
if (!file.isDirectory()) {
    final String fileName = file.getName();
    return getFilenameWithoutExtension(fileName);
return null;
StringgetFileNameWithoutExtension(final File file)
Returns file name without the extension.
if (file == null) {
    return null;
return stripExtension(file.getName());
StringgetFilenameWithoutExtension(final String fileName)
Gets the filename without the extension or null if the given file object is a directory.
final int ext_index = fileName.lastIndexOf(".");
final String fileNamePrefix;
if (ext_index != -1) {
    fileNamePrefix = fileName.substring(0, ext_index);
} else {
    fileNamePrefix = fileName;
return fileNamePrefix;
...
StringgetFileNameWithoutExtension(final String filePath)
returns file name "/tmp/abc.txt" returns "abc"
final File file = new File(filePath);
String fileName = file.getName();
fileName = fileName.replace(" ", "_");
final int index = fileName.lastIndexOf('.');
return fileName.substring(0, index);
StringgetFileNameWithoutExtension(String fileName)
Returns a file's name, without its extension.
int dotIndex = fileName.lastIndexOf('.');
return fileName.substring(0, dotIndex);
StringgetFileNameWithoutExtension(String filePath)
get File Name Without Extension
if (filePath == null || filePath.trim().length() == 0) {
    return filePath;
int extenPosi = filePath.lastIndexOf(".");
int filePosi = filePath.lastIndexOf(File.separator);
if (filePosi == -1) {
    return (extenPosi == -1 ? filePath : filePath.substring(0, extenPosi));
if (extenPosi == -1) {
    return filePath.substring(filePosi + 1);
return (filePosi < extenPosi ? filePath.substring(filePosi + 1, extenPosi)
        : filePath.substring(filePosi + 1));
StringgetFileNameWithoutExtension(String filePath)
get File Name Without Extension
StringBuffer sb = new StringBuffer();
String[] fileInfo = filePath.split(Pattern.quote(File.separator));
String[] splitByPeriod = fileInfo[fileInfo.length - 1].split("\\.");
for (int i = 0; i < splitByPeriod.length - 1; i++) {
    sb.append(splitByPeriod[i]);
return sb.toString();
StringgetFileNameWithoutExtension(String filePath)
get File Name Without Extension
return getFileNameWithoutExtension(new File(filePath));