Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the filename without the extension in Java?

Tags:

java

file

Can anyone tell me how to get the filename without the extension? Example:

fileNameWithExt = "test.xml"; fileNameWithOutExt = "test"; 
like image 910
Iso Avatar asked May 29 '09 04:05

Iso


People also ask

How can I find the file name without extension?

GetFileNameWithoutExtension(ReadOnlySpan<Char>) Returns the file name without the extension of a file path that is represented by a read-only character span.

How do I get the file extension of a file in Java?

If you are using the Gauva Library, you can directly use the getFileExtension() method to get the file extension. For example, String fileName = "Test. java"; String extension = Files.

How do I find a specific file in Java?

Searching files in Java can be performed using the File class and FilenameFilter interface. The FilenameFilter interface is used to filter files from the list of files. This interface has a method boolean accept(File dir, String name) that is implemented to find the desired files from the list returned by the java. io.


1 Answers

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); 
like image 189
Ulf Lindback Avatar answered Oct 07 '22 23:10

Ulf Lindback