This function is used to get the last element on a file path. - Java File Path IO

Java examples for File Path IO:Path

Description

This function is used to get the last element on a file path.

Demo Code


//package com.java2s;
import java.io.File;

import java.nio.file.Paths;

public class Main {
    /**//from  w  w  w  .  ja  v a  2  s  . c om
     * This function is used to get the last element on a file path. The last
     * element is the last file/ directory on the path.
     * 
     * @param path
     *       is the path under query.
     * @return
     *       the last element of the given path.
     */
    public static String getLastElementOfPath(String path) {
        return Paths.get(path).getFileName().toString();
    }

    public static String getLastElementOfPath(File path) {
        return getLastElementOfPath(path.toString());
    }
}

Related Tutorials