How to Get File Extension in Java With Examples

Sometimes while working with files, we need to process them differently based on their type. java.io.File doesn’t have any method to get the file extension, here I am providing a utility method to get the file extension in java.

Java Get File Extension

The extension of a file is the last part of its name after the period (.). For example, Java source file extension is “java” and you will notice that file name always ends with “.java”.

We can use this file name and extension logic to retrieve the last part of the file name and get the extension of the file.

Output of the above program is:

Note that here I am not checking if the file exists or not. However, in real programming scenarios, you should check that file exists or not before processing further.

By admin

Leave a Reply

%d bloggers like this: