In Java, you can find the file extension by using the File class and its getName() method to retrieve the name of the file. Then, you can use string manipulation methods such as lastIndexOf() and substring() to extract the file extension. Here's a simple example:
import java.io.File;
public class FileExtensionExample {
public static void main(String[] args) {
String fileName = "example.txt"; // Replace this with the actual file name
String fileExtension = getFileExtension(fileName);
System.out.println("File extension: " + fileExtension);
}
public static String getFileExtension(String fileName) {
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
return fileName.substring(fileName.lastIndexOf(".")+1);
} else {
return ""; // No file extension found
}
}
}
In this example, the getFileExtension() method takes the file name as input and returns the file extension. It checks if the file name contains a dot ('.') character, which indicates the presence of a file extension. If found, it extracts the substring after the last dot, which represents the file extension.