Java File ExtensionLast Updated : 17 Mar 2025 The File extension is the suffix attached to a computer file. It can be identified easily as it directly appears in the file name after a period (.) For example, consider a file name Demo.java, here the file name is Demo and .java is the file extension that represents the type of the file. In this section, we will learn how to get extension of file through a Java program. Java File Class
Getting File ExtensionThere are two ways to get file extension in Java: The following program uses the File class to identify the extension of the specified file that we give as input. FileTypeDemo.java Output: ![]() In the above Java program,
Using Apache Commons IOApache Commons IO is the component of Apache derived from Java API. They provide multiple methods to perform different operations on files without creating an object of the file. In the following program we have used the FilenameUtils.getExtension() method to get the file extension. GetFileExtensionApache,java Output: Extension: txt In the above Java program, getExtension() method of the FilenameUtils class is used to get the file extension without creating the file object. |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
