To get the filename without the extension in Java, you can use the getName()
method of the File
class to get the full filename, and then use the substring()
method of the String
class to extract the part of the string that represents the filename without the extension.
Here's an example of how you can get the filename without the extension in Java:
import java.io.File; // ... File file = new File("/path/to/file.txt"); String fullFilename = file.getName(); int index = fullFilename.lastIndexOf('.'); String filename = fullFilename.substring(0, index);
In this example, the file
object represents a file on the file system, and the fullFilename
variable holds the full filename, including the extension. The index
variable holds the index of the last occurrence of the .
character in the fullFilename
string, which represents the end of the filename and the beginning of the extension. The filename
variable is then obtained by extracting the part of the fullFilename
string from the beginning to the index
, using the substring()
method.