Java Program to Get the File Extension
What is File Extension ?
A file extension is the set of characters that appear after the last period (dot) in a file name and determine the type of file it is. The file extension provides information about the format of the file and the type of information it contains, which helps the operating system or software applications determine how to handle the file. For example, a file with the extension .txt is a plain text file, while a file with the extension .jpg is a JPEG image file.
File extensions and their associated file types include:
- .txt: plain text file
- .doc: Microsoft Word document
- .docx: Microsoft Word Open XML document
- .xls: Microsoft Excel spreadsheet
- .pdf: Portable Document Format file
- .jpg: JPEG image file
- .png: Portable Network Graphics image file
- .mp3: MP3 audio file
- .mp4: MP4 video file
- .java: Java source code file
Ways to Convert int into double :
String fileName = "example.txt"; int indexOfDot = fileName.lastIndexOf("."); String fileExtension = fileName.substring(indexOfDot + 1);
Explanation :
In this example, we use the lastIndexOf method to find the last occurrence of the dot (.) in the file name. We then use the substring method to extract the characters after the dot, which is the file extension.
String fileName = "example.txt"; String[] parts = fileName.split("\\."); String fileExtension = parts[parts.length - 1];
Explanation :
In this example, we use the split method to split the file name into an array of strings using the dot (.) as the delimiter. The last element of the array is the file extension.
import java.nio.file.Path; import java.nio.file.Paths; String fileName = "example.txt"; Path path = Paths.get(fileName); String fileExtension = path.getFileName().toString().split("\\.")[1];
Explanation :
In this example, we use the Paths.get method to create a Path object from the file name. We then use the getFileName method to get the FileName object, which represents the file name without the directory information. We use the toString method to get the string representation of the FileName object, and then split it using the split method to get the file extension.
Example : Using the substring() method
class Main { public static void main(String[] args) { String fileName = "example.txt"; int indexOfDot = fileName.lastIndexOf("."); String fileExtension = fileName.substring(indexOfDot + 1); System.out.println("File extension using substring method: " + fileExtension); } }
Output :
File extension using substring method: txt
Example : Using the split method
class Main { public static void main(String[] args) { String fileName = "example.txt"; String[] parts = fileName.split("\\."); String fileExtension = parts[parts.length - 1]; System.out.println("File extension using split method: " + fileExtension); } }
Output :
File extension using split method: txt
Example : Using the Path class
import java.nio.file.Path; import java.nio.file.Paths; class Main { public static void main(String[] args) { String fileName = "example.txt"; Path path = Paths.get(fileName); String fileExtension = path.getFileName().toString().split("\\.")[1]; System.out.println("File extension using Path class: " + fileExtension); } }
Output :
File extension using Path class: txt
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment