Returns the basename of the given file, that is the filename without the file-extension using Regex. - Java java.nio.file

Java examples for java.nio.file:Path

Description

Returns the basename of the given file, that is the filename without the file-extension using Regex.

Demo Code


//package com.java2s;

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**/*from w w w  . j  a va  2 s.  c  o  m*/
     * Returns the basename of the given file, that is the filename without the
     * file-extension.
     */
    public static String getBasename(String path) {
        return getBasename(Paths.get(path));
    }

    /**
     * Returns the basename of the given file, that is the filename without the
     * file-extension.
     */
    public static String getBasename(Path file) {
        if (file != null) {
            String filename = file.getFileName().toString();
            String[] tokens = filename.split("\\.(?=[^\\.]+$)");
            return tokens[0];
        }
        return null;
    }
}

Related Tutorials