In this case, use FilenameUtils.getExtension from Apache Commons IO

Here is an example of how to use it (you may specify either full path or just file name):

import org.apache.commons.io.FilenameUtils;

// ...

String ext1 = FilenameUtils.getExtension("/path/to/file/foo.txt"); // returns "txt"
String ext2 = FilenameUtils.getExtension("bar.exe"); // returns "exe"

Maven dependency:

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.6</version>
</dependency>

Gradle Groovy DSL

implementation 'commons-io:commons-io:2.6'

Gradle Kotlin DSL

implementation("commons-io:commons-io:2.6")

Others https://search.maven.org/artifact/commons-io/commons-io/2.6/jar

Answer from Juan Rojas on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › ext › basics › download.html
Download Extensions (The Java™ Tutorials > The Extension Mechanism > Creating and Using Extensions)
After generating a key, the jar files can be signed: jarsigner AreaApplet.jar test jarsigner area.jar test · You will be prompted for the keystore and key passwords. More information on keytool, jarsigner, and other security tools is at the Summary of Tools for the Java 2 Platform Security. Here is AreaDemo.html, which loads the applet and causes the extension code to be downloaded and installed:
🌐
Programiz
programiz.com › java-programming › examples › get-file-extension
Java Program to Get the File Extension
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.getFileExtension(fileName);
🌐
Mkyong
mkyong.com › home › java › how to get file extension in java
How to get file extension in Java - Mkyong.com
November 30, 2020 - Path: /path/../makefile -> File Extension: Path: /path/dir.test/makefile -> File Extension: The extra checking ensures the last file extension index (position) is always after the last file separator (Windows or Unix).
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Get File Extension From MIME Type in Java - Java Code Geeks
March 12, 2024 - Instantiate a MimeTypes object. Use the forName method to get the MimeType object corresponding to the MIME type. Retrieve the file extension using the getExtension method of the MimeType object.
🌐
Javatpoint
javatpoint.com › java-file-extension
Java File Extension - Javatpoint
How to Compress Image in Java Source Code · How to Download a File from a URL in Java · Passing an Object to The Method in Java · Permutation program in Java · Profile Annotation in Java · Scenario Based Questions in Java · Understanding Static Synchronization in Java ·
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › ext › basics › index.html
Lesson: Creating and Using Extensions (The Java™ Tutorials > The Extension Mechanism)
by placing the JAR file in a special location in the directory structure of the Java Runtime Environment, in which case it's called an installed extension. by referencing the JAR file in a specified way from the manifest of the another JAR file, in which case it's called a download extension.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-format
Java File Format | .java Extension - GeeksforGeeks
January 29, 2024 - Java is a high-level, object-oriented programming language. A source code file written in the Java programming language is saved with the extension .java. .Java file extension typically contains Java code that can be compiled into bytecode and executed on the Java Virtual Machine (JVM).
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-get-the-file-extension
Java Program to Get the File Extension - GeeksforGeeks
July 23, 2025 - // Java Program to Get the File Extension import java.io.*; import java.nio.file.Files; public class GFG { public static void main(String[] args) { // File location File f = new File("/home/mayur/GFG.java"); // If file exists if (f.exists()) { String fileType = "Undetermined"; String fileName = f.getName(); String extension = ""; int i = fileName.lastIndexOf('.'); if (i > 0) { extension = fileName.substring(i + 1); } try { fileType = Files.probeContentType(f.toPath()); } catch (IOException ioException) { System.out.println( "Cannot determine file type of " + f.getName() + " due to following exception: " + ioException); } // Print Extension System.out.println( "Extension used for file is -> " + extension + " and is probably " + fileType); } else { System.out.println("File does not exist!"); } } }
🌐
Edureka Community
edureka.co › home › community › categories › java › how can we get file extension in java
How can we get file extension in Java | Edureka Community
May 8, 2018 - How can we get file extension in Java · Why Choose Microsoft AZ-104 Certification Exam? Apr 30, 2025 · Mastering Supply Chain with SAP SCM Modules: Key Features and Functions Nov 8, 2024 · Can SAP Training with Placement Guarantee Your Dream Job? Nov 8, 2024 · Discover the Top Reasons to Choose SAP Training in Pune for Career Growth Nov 8, 2024 · Not able to download Six Sigma Certificate.
🌐
Solvusoft
solvusoft.com › en › file-extensions › file-extension-java
JAVA File Extension: What Is It & How To Open It?
December 14, 2025 - JAVA files are traditionally categorized as Developer Files. File Extension JAVA is supported by Windows, Mac, and Linux operating systems. These types of files can be found on both desktop and mobile devices.
🌐
Fileextension
fileextension.org › JAVA
File extension JAVA - Open the .JAVA file
The .JAVA file extension can correspond to several not related file formats. If there is more than one file format with the .JAVA extension, you will probably find most of them at this website. Remember that you need to download a program to support a particular file type.
🌐
File Info
fileinfo.com › extension › java
.JAVA File Extension - How to open .java files
May 24, 2016 - JAVA source code files are compiled into .CLASS files using a Java compiler (javac command). A CLASS file contains bytecode that can be executed by the Java Virtual Machine (JVM). The JVM can be downloaded for every major operating system, including Windows, macOS, and Linux.
🌐
Techcrashcourse
techcrashcourse.com › 2023 › 01 › java-program-to-get-file-extension.html
Java Program to Get the File Extension
In this program, we will learn to fetch the extension of a file. In Java, you can get the file extension of a file using the File class and the getName() and lastIndexOf() methods of the String class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-the-file-extension-in-java
How to get the file extension in Java? - GeeksforGeeks
February 20, 2024 - This technique makes use of stream features to separate the filename using the dot "." and obtain the part, which indicates the extension. This approach is compact and clear, for individuals who are familiar with streams. It can be combined with other stream operations. However it necessitates Java 8 or a newer version.
🌐
Tpoint Tech
tpointtech.com › java-file-extension
Java File Extension - Tpoint Tech
March 17, 2025 - How to Compress Image in Java Source Code · How to Download a File from a URL in Java · Passing an Object to The Method in Java · Permutation program in Java · Profile Annotation in Java · Scenario Based Questions in Java · Understanding Static Synchronization in Java ·