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
🌐
Programiz
programiz.com › java-programming › examples › get-file-extension
Java Program to Get the File Extension
import java.io.File; class Main ... file.toString(); int index = fileName.lastIndexOf('.'); if(index > 0) { String extension = fileName.substring(index + 1); System.out.println("File extension is " + extension); } } }...
🌐
Baeldung
baeldung.com › home › java › java io › how to get the file extension of a file in java
How to Get the File Extension of a File in Java | Baeldung
January 8, 2024 - Their corresponding file extensions are following: static final String EXPECTED_1 = "java"; static final String EXPECTED_2 = ""; static final String EXPECTED_3 = "gitignore"; static final String EXPECTED_4 = "log"; static final String EXPECTED_5 = ""; ...
🌐
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).
🌐
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 ... 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. This lesson shows you how the extension mechanism works by using a simple "toy" extension as an example...
🌐
Techcrashcourse
techcrashcourse.com › 2023 › 01 › java-program-to-get-file-extension.html
Java Program to Get the File Extension
import java.io.File; public class FileExtensionExample { public static void main(String[] args) { String fileName = "example.txt"; File file = new File(fileName); String fileNameWithoutExt = file.getName() .substring(0, file.getName().lastIndexOf(".")); String fileExtension = file.getName() .substring(file.getName().lastIndexOf(".") + 1); System.out.println("File name: " + fileNameWithoutExt); System.out.println("File extension: " + fileExtension); } } Output
🌐
Javatpoint
javatpoint.com › java-file-extension
Java File Extension - Javatpoint
Java File Extension with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
Find elsewhere
🌐
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 approach is concise and dependable although it assumes familiarity with the Path class. For developers who're comfortable with Java NIO libraries this method offers a convenient and efficient solution, for extracting file extensions. Example: In this example we will find the extension of the given file by getExtension() method.
🌐
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/foo.txt -> File Extension: txt Path: . -> File Extension: Path: .. -> File Extension: Path: /path/run.exe -> File Extension: exe Path: /path/makefile -> File Extension: Path: /path/.htaccess -> File Extension: htaccess Path: /path/.tar.gz -> File Extension: gz Path: /path/../makefile -> File Extension: Path: /path/dir.test/makefile -> File Extension: ... Founder of Mkyong.com, passionate Java and open-source technologies.
🌐
JanBask Training
janbasktraining.com › community › java › what-is-the-extension-of-java-code-files
What is the extension of Java code files? | JanBask Training Community
January 15, 2024 - When you will save the above code to your file, you will get the name of the file in “Myclass.java”. This “.java” extension would prefer that it be the java source code file.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-get-the-file-extension
Java Program to Get the File Extension - GeeksforGeeks
July 23, 2025 - The extension of a file is the last part of its name after the period (.). For example, the Java source file extension is “java”, and you will notice that the file name always ends with “.java”. Getting the file extension in Java is ...
🌐
Mkyong
mkyong.com › home › java › how to find files with the file extension in java
How to find files with the file extension in Java - Mkyong.com
December 1, 2020 - This example finds files matched with png file extension. The finds start at the top-level folder C:\\test and included all levels of subfolders. ... package com.mkyong.io.howto; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class FindFileByExtension1 { public static void main(String[] args) { try { List<String> files = findFiles(Paths.get("C:\\test"), "png"); files.forEach(x -> System.out.println(x)); } catch (IOException e) { e.pri
🌐
Oracle
docs.oracle.com › javase › tutorial › ext › index.html
Trail: The Extension Mechanism (The Java™ Tutorials)
The Packaging Programs in JAR Files lesson in this tutorial. The JAR Guide in the JDK™ documentation. ... This section shows you what you need to do to add an extension to your Java platform and how applets can benefit from the extension mechanism by downloading remote extension classes.
🌐
Filext
filext.com › file-extension › JAVA
JAVA File Extension - What is it? How to open a JAVA file?
It saves Java source code in a plain text JAVA file. This file format is classified as Developer. If you know the file format, you can also find out how to open or edit the file. Each file format has a unique extension and almost always a unique signature. For example, Microsoft Word documents have the extension .docx and the signature (usually the first 3 characters in this file) PK.
🌐
Delft Stack
delftstack.com › home › howto › java › how to get the file extension in java
How to Get the File Extension of a File in Java | Delft Stack
February 2, 2024 - The contains() method is used to check whether the specified char is present in the string or not and the lastIndexOf() method returns an index value of the specified char which is passed into substring() method to get file extension. We use these methods in this code to get file extension. See the example below.
🌐
W3Docs
w3docs.com › java
How do I get the file extension of a file in Java? | W3Docs
To get the file extension of a file in Java, you can use the File class and its getName() method to get the file name, and then use the substring() method of the String class to extract the extension from the file name. Here is an example of how you can do this: java ·
🌐
Lifewire
lifewire.com › what-is-a-java-file-2622759
What's a JAVA File and How Do You Open One?
July 20, 2023 - It's possible you're not actually dealing with a JAVA file, but instead a file that ​uses a similarly spelled file extension. For example, the AVA suffix looks a bit like JAVA, but it's used for eBook with Persian AvaaPlayer. JA files are ...
🌐
Scaler
scaler.com › home › topics › java file extension
Java File Extension - Scaler Topics
January 13, 2024 - The .java extension indicates that the file contains source code written in the Java programming language. These files function as blueprints for software programs, instructing the computer on how to carry out tasks. The extension in Java separates these text-based files from compiled ones, making it easier for developers to organize and manage their codebase.