Reputation: 3758
Can anyone tell me how to get the filename without the extension? Example:
fileNameWithExt = "test.xml";
fileNameWithOutExt = "test";
Upvotes: 348
Views: 412268
Reputation: 7710
If you don't like to import the full apache.commons
, I've extracted the same functionality:
public class StringUtils {
public static String getBaseName(String filename) {
return removeExtension(getName(filename));
}
public static int indexOfLastSeparator(String filename) {
if(filename == null) {
return -1;
} else {
int lastUnixPos = filename.lastIndexOf(47);
int lastWindowsPos = filename.lastIndexOf(92);
return Math.max(lastUnixPos, lastWindowsPos);
}
}
public static String getName(String filename) {
if(filename == null) {
return null;
} else {
int index = indexOfLastSeparator(filename);
return filename.substring(index + 1);
}
}
public static String removeExtension(String filename) {
if(filename == null) {
return null;
} else {
int index = indexOfExtension(filename);
return index == -1?filename:filename.substring(0, index);
}
}
public static int indexOfExtension(String filename) {
if(filename == null) {
return -1;
} else {
int extensionPos = filename.lastIndexOf(46);
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos?-1:extensionPos;
}
}
}
Upvotes: 8
Reputation: 301
Just put following methods in an helper class.
public static String getFilenameWithoutExtension(final File file) {
final String filename = file.getName();
final int pointIndex = filename.lastIndexOf('.');
if (pointIndex > 0) {
return filename.substring(0, pointIndex);
} else {
return filename;
}
}
public static String getFilenameWithoutExtension(String filePath) {
int charIndex = filePath.lastIndexOf(java.nio.file.FileSystems.getDefault().getSeparator());
if (charIndex > 0) {
filePath = filePath.substring(charIndex + 1);
}
charIndex = filePath.lastIndexOf('.');
if (charIndex > 0) {
return filePath.substring(0, charIndex);
} else {
return filePath;
}
}
Upvotes: 0
Reputation: 4647
If your project uses Spring you can use:
import org.springframework.util.StringUtils;
// …
StringUtils.stripFilenameExtension("/tmp/myFile.txt") // → "/tmp/myFile"
NB: This method has its limitations when it comes to dotfiles, where it will return /tmp/
for /tmp/.config
(getBaseName()
from commons-io has the same problem btw).
This Baeldung article proposes a nifty two-liner that claims to work for all cases:
public static String removeFileExtension(String filename, boolean removeAllExtensions) {
if (filename == null || filename.isEmpty()) return filename;
return filename.replaceAll("(?<!^)[.]" + (removeAllExtensions ? ".*" : "[^.]*$"), "");
}
Upvotes: 0
Reputation: 7679
file name only, where full path is also included. No need for external libs, regex...etc
public class MyClass {
public static void main(String args[]) {
String file = "some/long/directory/blah.x.y.z.m.xml";
System.out.println(file.substring(file.lastIndexOf("/") + 1, file.lastIndexOf(".")));
//outputs blah.x.y.z.m
}
}
Upvotes: 0
Reputation: 261
The fluent way:
public static String fileNameWithOutExt (String fileName) {
return Optional.of(fileName.lastIndexOf(".")).filter(i-> i >= 0)
.filter(i-> i > fileName.lastIndexOf(File.separator))
.map(i-> fileName.substring(0, i)).orElse(fileName);
}
Upvotes: 1
Reputation: 131
Given the String filename
, you can do:
String filename = "test.xml";
filename.substring(0, filename.lastIndexOf(".")); // Output: test
filename.split("\\.")[0]; // Output: test
Upvotes: 4
Reputation: 473
com.google.common.io.Files
Files.getNameWithoutExtension(sourceFile.getName())
can do a job as well
Upvotes: 0
Reputation: 61
fileEntry.getName().substring(0, fileEntry.getName().lastIndexOf("."));
Upvotes: 3
Reputation: 2682
Here is the consolidated list order by my preference.
Using apache commons
import org.apache.commons.io.FilenameUtils;
String fileNameWithoutExt = FilenameUtils.getBaseName(fileName);
OR
String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);
Using Google Guava (If u already using it)
import com.google.common.io.Files;
String fileNameWithOutExt = Files.getNameWithoutExtension(fileName);
Or using Core Java
1)
String fileName = file.getName();
int pos = fileName.lastIndexOf(".");
if (pos > 0 && pos < (fileName.length() - 1)) { // If '.' is not the first or last character.
fileName = fileName.substring(0, pos);
}
if (fileName.indexOf(".") > 0) {
return fileName.substring(0, fileName.lastIndexOf("."));
} else {
return fileName;
}
private static final Pattern ext = Pattern.compile("(?<=.)\\.[^.]+$");
public static String getFileNameWithoutExtension(File file) {
return ext.matcher(file.getName()).replaceAll("");
}
Liferay API
import com.liferay.portal.kernel.util.FileUtil;
String fileName = FileUtil.stripExtension(file.getName());
Upvotes: 83
Reputation: 362
For Kotlin it's now simple as:
val fileNameStr = file.nameWithoutExtension
Upvotes: 7
Reputation: 73
My solution needs the following import.
import java.io.File;
The following method should return the desired output string:
private static String getFilenameWithoutExtension(File file) throws IOException {
String filename = file.getCanonicalPath();
String filenameWithoutExtension;
if (filename.contains("."))
filenameWithoutExtension = filename.substring(filename.lastIndexOf(System.getProperty("file.separator"))+1, filename.lastIndexOf('.'));
else
filenameWithoutExtension = filename.substring(filename.lastIndexOf(System.getProperty("file.separator"))+1);
return filenameWithoutExtension;
}
Upvotes: 0
Reputation: 538
You can use java split function to split the filename from the extension, if you are sure there is only one dot in the filename which for extension.
File filename = new File('test.txt');
File.getName().split("[.]");
so the split[0]
will return "test" and split[1] will return "txt"
Upvotes: 2
Reputation: 1293
Keeping it simple, use Java's String.replaceAll() method as follows:
String fileNameWithExt = "test.xml";
String fileNameWithoutExt
= fileNameWithExt.replaceAll( "^.*?(([^/\\\\\\.]+))\\.[^\\.]+$", "$1" );
This also works when fileNameWithExt includes the fully qualified path.
Upvotes: 0
Reputation: 21
Simplest way to get name from relative path or full path is using
import org.apache.commons.io.FilenameUtils;
FilenameUtils.getBaseName(definitionFilePath)
Upvotes: 2
Reputation: 12757
Use FilenameUtils.removeExtension
from Apache Commons IO
Example:
You can provide full path name or only the file name.
String myString1 = FilenameUtils.removeExtension("helloworld.exe"); // returns "helloworld"
String myString2 = FilenameUtils.removeExtension("/home/abc/yey.xls"); // returns "yey"
Hope this helps ..
Upvotes: 1
Reputation: 3868
public static String getFileExtension(String fileName) {
if (TextUtils.isEmpty(fileName) || !fileName.contains(".") || fileName.endsWith(".")) return null;
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
public static String getBaseFileName(String fileName) {
if (TextUtils.isEmpty(fileName) || !fileName.contains(".") || fileName.endsWith(".")) return null;
return fileName.substring(0,fileName.lastIndexOf("."));
}
Upvotes: 1
Reputation: 591
You can split it by "." and on index 0 is file name and on 1 is extension, but I would incline for the best solution with FileNameUtils from apache.commons-io like it was mentioned in the first article. It does not have to be removed, but sufficent is:
String fileName = FilenameUtils.getBaseName("test.xml");
Upvotes: 0
Reputation: 6011
Below is reference from https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/util/FileUtil.java
/**
* Gets the base name, without extension, of given file name.
* <p/>
* e.g. getBaseName("file.txt") will return "file"
*
* @param fileName
* @return the base name
*/
public static String getBaseName(String fileName) {
int index = fileName.lastIndexOf('.');
if (index == -1) {
return fileName;
} else {
return fileName.substring(0, index);
}
}
Upvotes: 10
Reputation: 881093
See the following test program:
public class javatemp {
static String stripExtension (String str) {
// Handle null case specially.
if (str == null) return null;
// Get position of last '.'.
int pos = str.lastIndexOf(".");
// If there wasn't any '.' just return the string as is.
if (pos == -1) return str;
// Otherwise return the string, up to the dot.
return str.substring(0, pos);
}
public static void main(String[] args) {
System.out.println ("test.xml -> " + stripExtension ("test.xml"));
System.out.println ("test.2.xml -> " + stripExtension ("test.2.xml"));
System.out.println ("test -> " + stripExtension ("test"));
System.out.println ("test. -> " + stripExtension ("test."));
}
}
which outputs:
test.xml -> test
test.2.xml -> test.2
test -> test
test. -> test
Upvotes: 59
Reputation: 14170
If you, like me, would rather use some library code where they probably have thought of all special cases, such as what happens if you pass in null or dots in the path but not in the filename, you can use the following:
import org.apache.commons.io.FilenameUtils;
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
Upvotes: 533
Reputation: 81751
If your project uses Guava (14.0 or newer), you can go with Files.getNameWithoutExtension()
.
(Essentially the same as FilenameUtils.removeExtension()
from Apache Commons IO, as the highest-voted answer suggests. Just wanted to point out Guava does this too. Personally I didn't want to add dependency to Commons—which I feel is a bit of a relic—just because of this.)
Upvotes: 47
Reputation: 25
Try the code below. Using core Java basic functions. It takes care of String
s with extension, and without extension (without the '.'
character). The case of multiple '.'
is also covered.
String str = "filename.xml";
if (!str.contains("."))
System.out.println("File Name=" + str);
else {
str = str.substring(0, str.lastIndexOf("."));
// Because extension is always after the last '.'
System.out.println("File Name=" + str);
}
You can adapt it to work with null
strings.
Upvotes: -3
Reputation: 25673
While I am a big believer in reusing libraries, the org.apache.commons.io JAR is 174KB, which is noticably large for a mobile app.
If you download the source code and take a look at their FilenameUtils class, you can see there are a lot of extra utilities, and it does cope with Windows and Unix paths, which is all lovely.
However, if you just want a couple of static utility methods for use with Unix style paths (with a "/" separator), you may find the code below useful.
The removeExtension
method preserves the rest of the path along with the filename. There is also a similar getExtension
.
/**
* Remove the file extension from a filename, that may include a path.
*
* e.g. /path/to/myfile.jpg -> /path/to/myfile
*/
public static String removeExtension(String filename) {
if (filename == null) {
return null;
}
int index = indexOfExtension(filename);
if (index == -1) {
return filename;
} else {
return filename.substring(0, index);
}
}
/**
* Return the file extension from a filename, including the "."
*
* e.g. /path/to/myfile.jpg -> .jpg
*/
public static String getExtension(String filename) {
if (filename == null) {
return null;
}
int index = indexOfExtension(filename);
if (index == -1) {
return filename;
} else {
return filename.substring(index);
}
}
private static final char EXTENSION_SEPARATOR = '.';
private static final char DIRECTORY_SEPARATOR = '/';
public static int indexOfExtension(String filename) {
if (filename == null) {
return -1;
}
// Check that no directory separator appears after the
// EXTENSION_SEPARATOR
int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
int lastDirSeparator = filename.lastIndexOf(DIRECTORY_SEPARATOR);
if (lastDirSeparator > extensionPos) {
LogIt.w(FileSystemUtil.class, "A directory separator appears after the file extension, assuming there is no file extension");
return -1;
}
return extensionPos;
}
Upvotes: 5
Reputation: 29852
The easiest way is to use a regular expression.
fileNameWithOutExt = "test.xml".replaceFirst("[.][^.]+$", "");
The above expression will remove the last dot followed by one or more characters. Here's a basic unit test.
public void testRegex() {
assertEquals("test", "test.xml".replaceFirst("[.][^.]+$", ""));
assertEquals("test.2", "test.2.xml".replaceFirst("[.][^.]+$", ""));
}
Upvotes: 192