Open In App

How to Extract File Extension From a File Path String in Java?

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:




import java.io.File;
  
public class Code {
    public static void main(String[] args)
    {
        String filepath = "example.txt";
        // Print filename and extension for the given
        // filepath
        printFileNameAndExtension(filepath);
  
        filepath = "/path/to/your/file/example.pdf";
        printFileNameAndExtension(filepath);
  
        filepath = "no_extension_file";
        printFileNameAndExtension(filepath);
    }
  
    // Function to print filename and extension for the
    // given filepath
    public static void printFileNameAndExtension(String filepath)
    {
        // Print filename and extension for the given
        // filepath
        System.out.println("Filename and Extension: "
                           + filepath);
        // Get and print the extension for the given
        // filepath
        String extension = getFileExtension(filepath);
        System.out.println("Extension: " + extension);
    }
  
    // Function to get the extension from a file path
    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:

Usage:

2. Extracting File Extensions Using Java’s File Class

Implementation:




import java.io.File;
  
// Driver Class
public class Code2 {
      // Main Function
    public static void main(String[] args) {
        String filepath = "example.txt";
        
          // It will print file name and extension
        printFileNameAndExtension(filepath);  
          
        filepath = "/path/to/your/file/example.pdf";
        
      //It will print filename and extension for given filepath
        printFileNameAndExtension(filepath);   
          
        filepath = "no_extension_file";
        printFileNameAndExtension(filepath);
    }
  
   // function for printing file name and extension 
    public static void printFileNameAndExtension(String filepath) 
    {
        System.out.println("Filename and Extension: " + filepath);
        
        String ans = getFileExtension(filepath);    
        System.out.println("Extension: " + ans);  
    }
  
   // function to get the extension from path
    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:

Usage:

3. Extracting File Extensions Using Java NIO’s Path class

Implementation:




import java.nio.file.Path;
import java.nio.file.Paths;
  
public class Code3 {
    public static void main(String[] args) 
    {
        String filepath = "example.txt";
        // Print filename and extension for the given filepath
        printFileNameAndExtension(filepath);
          
        filepath = "/path/to/your/file/example.pdf";
        printFileNameAndExtension(filepath);
          
        filepath = "no_extension_file";
        printFileNameAndExtension(filepath);
    }
  
    // Function to print filename and extension for the given filepath
    public static void printFileNameAndExtension(String filepath) {
        // Convert filepath to Path
        Path path = Paths.get(filepath);
        // Get the filename from the Path
        String fileName = path.getFileName().toString();
        // Print filename
        System.out.println("Filename and Extension: " + fileName);
        // Get and print the extension for the filename
        String extension = getFileExtension(fileName);
        System.out.println("Extension: " + extension);  
    }
  
    // Function to get the extension from a filename
    public static String getFileExtension(String fileName) {
        // Find the last occurrence of '.' in the filename
        int dotIndex = fileName.lastIndexOf('.');
        // If '.' is not found, return "No extension", otherwise return the substring after '.'
        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:

Usage:


Article Tags :