Returns the file extension of the given file using Regex. - Java java.nio.file

Java examples for java.nio.file:Path

Description

Returns the file extension of the given file using Regex.

Demo Code


//package com.java2s;

import java.nio.file.Path;

public class Main {
    /**/*from   ww  w .jav  a  2s .co  m*/
     * Returns the file extension of the given file.
     */
    public static String getExtension(Path file) {
        if (file != null) {
            String filename = file.getFileName().toString();
            String[] tokens = filename.split("\\.(?=[^\\.]+$)");
            return tokens.length > 1 ? tokens[1] : "";
        }
        return null;
    }
}

Related Tutorials