Get filename without extension in Java
This post will discuss how to get the filename without extension in Java. The solution should return the file name before the last dot.
1. Using String.substring() method
The idea is to use the lastIndexOf() method to get the index of the last occurrence of the dot (.) in the given file name. Then you can extract the filename without extension using the substring() method, as shown below:
|
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { String fileName = "filename.txt"; String fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.')); System.out.println(fileNameWithoutExtension); } } |
The above code throws a java.lang.StringIndexOutOfBoundsException if the file name has no extension. You can handle this case by checking the return value of the lastIndexOf() method, which returns -1 if the dot does not occur. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Main { private static String removeExtension(String fileName) { int lastIndex = fileName.lastIndexOf('.'); if (lastIndex != -1) { fileName = fileName.substring(0, lastIndex); } return fileName; } public static void main(String[] args) { String fileName = "filename.txt"; String fileNameWithoutExtension = removeExtension(fileName); System.out.println(fileNameWithoutExtension); } } |
2. Using Apache Commons IO
Another option is to use the FilenameUtils class provided by Apache Commons IO library. It provides the removeExtension() method to removes the extension from a filename. If the fileName contains a path, it doesn’t remove it.
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.io.FilenameUtils; public class Main { public static void main(String[] args) { String fileName = "filename.txt"; String fileNameWithoutExtension = FilenameUtils.removeExtension(fileName); System.out.println(fileNameWithoutExtension); } } |
Alternatively, you can also use the getBaseName() method from the FilenameUtils class that additionally removes the path from the filename (if present), along with the extension. It is similar to the basename Unix command.
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.io.FilenameUtils; public class Main { public static void main(String[] args) { String fileName = "filename.txt"; String fileNameWithoutExtension = FilenameUtils.getBaseName(fileName); System.out.println(fileNameWithoutExtension); } } |
3. Using Guava
If your project uses the Guava library over Apache Commons, consider using the getNameWithoutExtension() method from the Files class. It returns the file name without its extension or path, similar to the basename Unix command.
|
1 2 3 4 5 6 7 8 9 10 |
import com.google.common.io.Files; public class Main { public static void main(String[] args) { String fileName = "filename.txt"; String fileNameWithoutExtension = Files.getNameWithoutExtension(fileName); System.out.println(fileNameWithoutExtension); } } |
4. Using Regex
Another option is to use the regular expression \.\w+$ with the replaceAll() method that removes the last dot and all the characters that follow it.
|
1 2 3 4 5 6 7 8 |
public class Main { public static void main(String[] args) { String fileName = "filename.txt"; String fileNameWithoutExtension = fileName.replaceAll("\\.\\w+$", ""); System.out.println(fileNameWithoutExtension); } } |
That’s all about getting the filename without extension in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)