path Name - Java File Path IO

Java examples for File Path IO:Path

Description

path Name

Demo Code


//package com.java2s;

public class Main {
    public static final String SEPARATOR = "/";

    public static String pathName(String path) {
        String[] split = cleanPath(path).split(SEPARATOR);
        if (split.length > 0) {
            return split[split.length - 1];
        }//  w  w  w  .j  a va2  s.c o m
        return null;
    }

    /**
     * Clean the path string by removing leading and trailing slashes and removing duplicate slashes.
     * @param path input path
     * @return cleaned path string
     */
    public static String cleanPath(String path) {
        if (path.endsWith(SEPARATOR)) {
            path = path.replaceAll(SEPARATOR + "+$", "");
        }
        if (path.startsWith(SEPARATOR)) {
            path = path.replaceAll("^" + SEPARATOR + "+", "");
        }
        return path.replaceAll("/+", SEPARATOR);
    }
}

Related Tutorials