How to get File type in Java?

One essential part of file processing in Java is figuring out the kind of file, which is frequently used in a variety of applications. Comprehending the kind of file is essential for executing particular tasks or verifications depending on the contents or extension of the file.

It can be done in Java in several ways, such as by looking at file extensions, using built-in Java classes like File and Path, or using libraries like Guava or Apache Commons IO. In this section, we will discuss various methods for obtaining file types in Java, including suggestions for useful implementations and things to think about while handling files in Java programs.

Java Program to Get the File Extension

1. Using lastIndexOf() and substring() Method

FileExtension.java

Output:

txt

Explanation

The File object is transformed into a string using file.toString(). File extensions are separated by the '.' character, which is found the last time it appears using fn.lastIndexOf('.').

2. File Extensions of All Files in a Directory

FileExtension.java

Output:

Files                   Extension
D:\TTP Documentation\1. Cover Page and First Page.docx  docx
D:\TTP Documentation\132_camerar.docx   docx
D:\TTP Documentation\132_copyr.docx     docx
D:\TTP Documentation\132_Paper.pptx     pptx
D:\TTP Documentation\2.Certificate.docx docx
D:\TTP Documentation\3.Candidate's Declaration.docx     docx
D:\TTP Documentation\384_camerar[1].docx        docx
D:\TTP Documentation\4.Acknowledgement.docx     docx
D:\TTP Documentation\6.Contents Page.docx       docx
D:\TTP Documentation\Adobe Scan 04 Apr 2023.pdf pdf
D:\TTP Documentation\CICTN_132.pptx     pptx
D:\TTP Documentation\Text To Face Generation .docx      docx
D:\TTP Documentation\Text To Face Generation.pdf        pdf
PS C:\Users\Master's>

Explanation

The Java program defines a directory path, lists files in the specified directory ("D:\TTP Documentation"), and prints each file's name alongside its extension. It handles cases where the directory doesn't exist, providing a clear output of filenames and extensions.

3. Using Guava Library

The Guava library makes the process of extracting file extensions easier with its Files.getFileExtension(String fileName) function. It internally manages locating the final instance of the '.' character and extracting the substring that denotes the file extension when given a fileName as input.

FileExtension.java

Output:

Files                   Extension
D:\TTP Documentation\1. Cover Page and First Page.docx  docx
D:\TTP Documentation\132_camerar.docx   docx
D:\TTP Documentation\132_copyr.docx     docx
D:\TTP Documentation\132_Paper.pptx     pptx
D:\TTP Documentation\2.Certificate.docx docx
D:\TTP Documentation\3.Candidate's Declaration.docx     docx
D:\TTP Documentation\384_camerar[1].docx        docx
D:\TTP Documentation\4.Acknowledgement.docx     docx
D:\TTP Documentation\5.Abstract.docx    docx
D:\TTP Documentation\59c22cf8b853e_Copyright form.pdf   pdf
D:\TTP Documentation\6.Contents Page.docx       docx
D:\TTP Documentation\Adobe Scan 04 Apr 2023.pdf pdf
D:\TTP Documentation\CICTN_132.pptx     pptx
D:\TTP Documentation\seminar
D:\TTP Documentation\Text To Face Generation .docx      docx
D:\TTP Documentation\Text To Face Generation.pdf        pdf

Explanation

To access files in the designated location, the code initializes a File object reflecting the directory path "D:\TTP Documentation".Iterating through the files in the directory and transforming each file name to a string before extracting the file extension using a proprietary method (getFileExtension). It generates a list of files and the extensions that go with them by printing each file's name and extension in a tabular manner.






Latest Courses