How to Extract File Extension From a File Path String in Java?
Last Updated :
01 Feb, 2024
In Java, working with Files is common, and knowing how to extract file extensions from file paths is essential for making informed decisions based on file types.
In this article, we will explore techniques for doing this efficiently, empowering developers to improve their file-related operations.
Program to extract file extension from a file path String in Java
Below are the three methods and their implementations to extract file extension from a file path String.
1. A Simple Method in Java
Implementation:
Java
import java.io.File;
public class Code {
public static void main(String[] args)
{
String filepath = "example.txt" ;
printFileNameAndExtension(filepath);
filepath = "/path/to/your/file/example.pdf" ;
printFileNameAndExtension(filepath);
filepath = "no_extension_file" ;
printFileNameAndExtension(filepath);
}
public static void printFileNameAndExtension(String filepath)
{
System.out.println( "Filename and Extension: "
+ filepath);
String extension = getFileExtension(filepath);
System.out.println( "Extension: " + extension);
}
public static String getFileExtension(String filePath)
{
int lastIndexOfDot = filePath.lastIndexOf( '.' );
if (lastIndexOfDot == - 1 ) {
return "No extension" ;
}
return filePath.substring(lastIndexOfDot + 1 );
}
}
|
Output
Filename and Extension: example.txt
Extension: txt
Filename and Extension: /path/to/your/file/example.pdf
Extension: pdf
Filename and Extension: no_extension_file
Extension: No extension
Method Overview:
- The method getFileExtension takes a file path as input.
- It utilizes the lastIndexOf method to find the last occurrence of the dot (‘.’) character in the file path.
- If no dot is found (lastIndexOfDot == -1), it means there is no file extension, and “No extension” is returned.
- Otherwise, it extracts the substring from the index after the last dot to the end of the file path, effectively giving the file extension.
Usage:
- Example file paths are provided to showcase the method in action.
- The results are printed, demonstrating how the method accurately extracts file extensions.
- Handles cases where no extension is present, returning an appropriate message.
2. Extracting File Extensions Using Java’s File Class
Implementation:
Java
import java.io.File;
public class Code2 {
public static void main(String[] args) {
String filepath = "example.txt" ;
printFileNameAndExtension(filepath);
filepath = "/path/to/your/file/example.pdf" ;
printFileNameAndExtension(filepath);
filepath = "no_extension_file" ;
printFileNameAndExtension(filepath);
}
public static void printFileNameAndExtension(String filepath)
{
System.out.println( "Filename and Extension: " + filepath);
String ans = getFileExtension(filepath);
System.out.println( "Extension: " + ans);
}
public static String getFileExtension(String filePath) {
String fileName = new File(filePath).getName();
int dotIndex = fileName.lastIndexOf( '.' );
return (dotIndex == - 1 ) ? "No extension" : fileName.substring(dotIndex + 1 );
}
}
|
Output
Filename and Extension: example.txt
Extension: txt
Filename and Extension: /path/to/your/file/example.pdf
Extension: pdf
Filename and Extension: no_extension_file
Extension: No extension
Method Overview:
- Utilizes the File class to obtain the file name from the given file path.
- Uses lastIndexOf method to find the last occurrence of the dot (‘.’) character in the file name.
- If no dot is found (dotIndex == -1), it returns “No extension.”
- Otherwise, it extracts the substring from the index after the last dot to the end of the file name, effectively giving the file extension.
Usage:
- Demonstrates the method’s effectiveness in extracting file extensions.
- Handles cases where no extension is present, providing a clear and appropriate message.
3. Extracting File Extensions Using Java NIO’s Path class
Implementation:
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class Code3 {
public static void main(String[] args)
{
String filepath = "example.txt" ;
printFileNameAndExtension(filepath);
filepath = "/path/to/your/file/example.pdf" ;
printFileNameAndExtension(filepath);
filepath = "no_extension_file" ;
printFileNameAndExtension(filepath);
}
public static void printFileNameAndExtension(String filepath) {
Path path = Paths.get(filepath);
String fileName = path.getFileName().toString();
System.out.println( "Filename and Extension: " + fileName);
String extension = getFileExtension(fileName);
System.out.println( "Extension: " + extension);
}
public static String getFileExtension(String fileName) {
int dotIndex = fileName.lastIndexOf( '.' );
return (dotIndex == - 1 ) ? "No extension" : fileName.substring(dotIndex + 1 );
}
}
|
Output
Filename and Extension: example.txt
Extension: txt
Filename and Extension: example.pdf
Extension: pdf
Filename and Extension: no_extension_file
Extension: No extension
Method Overview:
- Paths.get(String) Method: Creates a Path object representing the file or directory located at the specified path.
- Path.getFileName() Method: Retrieves the file or directory name represented by this Path.
- String.lastIndexOf(char) Method: Returns the index of the last occurrence of the specified character, or 1 if the character is not found.
- String.substring(int) Method: Returns a new string that is a substring of this string, starting from the specified index.
Usage:
- Creating Path Object: Use Paths.get(String) to create a Path object representing the file or directory.
- Extracting File Name: Call getFileName() on the Path object to obtain the file or directory name as a string.
- Finding Extension: Use lastIndexOf(‘.’) to locate the last occurrence of the dot (.) in the file name, indicating the start of the file extension.
- Extracting Extension: Apply substring(dotIndex + 1) to get the substring starting from the index after the dot, representing the file extension.
- Handling No Extension: Check if lastIndexOf(‘.’) returns 1; if true, there is no extension, and handle accordingly (e.g., set a default message or process).
Share your thoughts in the comments
Please Login to comment...