Languages
[Edit]
EN

Java - get file extension

6 points
Created by:
Maggotta
499

In this article, we would like to show you how to get a file extension in Java.

Practical example:

import java.io.*;

public class Program {

    public static void main(String[] args) {
        
        // Common:

        System.out.println(PathUtils.getExtension("image.png"));             // png
        System.out.println(PathUtils.getExtension("audio.mp3"));             // mp3
        System.out.println(PathUtils.getExtension("document.pdf"));          // pdf

        System.out.println(PathUtils.getExtension("."));                     //
        System.out.println(PathUtils.getExtension(".."));                    //

        System.out.println(PathUtils.getExtension(".htaccess"));             //
        System.out.println(PathUtils.getExtension(".tar.gz"));               // gz
        System.out.println(PathUtils.getExtension("makefile"));              //

        // Unix/Linux/macOS:

        System.out.println(PathUtils.getExtension("/path/.htaccess"));       //
        System.out.println(PathUtils.getExtension("/path/.tar.gz"));         // gz
        System.out.println(PathUtils.getExtension("/path/makefile"));        //
        System.out.println(PathUtils.getExtension("/path/example.pdf"));     // pdf

        // Windows:

        System.out.println(PathUtils.getExtension("C:\\path\\.htaccess"));   //
        System.out.println(PathUtils.getExtension("C:\\path\\.tar.gz"));     // gz
        System.out.println(PathUtils.getExtension("C:\\path\\makefile"));    //
        System.out.println(PathUtils.getExtension("C:\\path\\example.pdf")); // pdf

        // Also:

        System.out.println(PathUtils.getExtension("C:.htaccess"));           //
        System.out.println(PathUtils.getExtension("C:.tar.gz"));             // gz
        System.out.println(PathUtils.getExtension("C:makefile"));            //
        System.out.println(PathUtils.getExtension("C:example.pdf"));         // pdf
    }
}

Output:

png
mp3
pdf



gz


gz

pdf

gz

pdf

gz

pdf

 

PathUtils.java file:

public final class PathUtils {

    public static String getExtension(String path) {
        int length = path.length();
        for (int i = length - 1; i > -1; --i) {
            char value = path.charAt(i);
            if (value == '.') {
                if (i > 1) {
                    value = path.charAt(i - 1);
                    if (value == '/' || value == '\\' || value == ':') {
                        return "";
                    }
                    return path.substring(i + 1);
                }
                return "";
            }
            if (value == '/' || value == '\\' || value == ':') {
                return "";
            }
        }
        return "";
    }
}

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join