Java Path File Name nio getSimpleName(String path)

Here you can find the source of getSimpleName(String path)

Description

Get the name of a file.

License

Open Source License

Parameter

Parameter Description
path The path to the file.

Return

The file name.

Declaration

public static String getSimpleName(String path) 

Method Source Code


//package com.java2s;
// The MIT License(MIT)

import java.net.URI;

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

public class Main {
    /**/*  w w  w . j a v  a  2s  .co  m*/
     * <p>
     * Get the name of a file.
     * </p>
     *
     * @param uri The {@link URI} of the file.
     * @return The file name.
     */
    public static String getSimpleName(URI uri) {
        return getSimpleName(Paths.get(uri));
    }

    /**
     * <p>
     * Get the name of a file.
     * </p>
     *
     * @param path The path to the file.
     * @return The file name.
     */
    public static String getSimpleName(String path) {
        return getSimpleName(Paths.get(path));
    }

    private static String getSimpleName(Path path) {
        String fileName = getName(path);

        if (fileName != null) {
            return fileName.substring(0, fileName.lastIndexOf('.'));
        }

        return null;
    }

    /**
     * <p>
     * Get the name and extension of a file.
     * </p>
     *
     * @param uri The {@link URI} of the file.
     * @return The file name and extension.
     */
    public static String getName(URI uri) {
        return getName(Paths.get(uri));
    }

    /**
     * <p>
     * Get the name and extension of a file
     * </p>
     *
     * @param path The path to the file.
     * @return The file name and extension.
     */
    public static String getName(String path) {
        return getName(Paths.get(path));
    }

    private static String getName(Path path) {
        Path fileName = path.getFileName();

        if (fileName != null) {
            return fileName.toString();
        }

        return null;
    }
}

Related

  1. getRelativePathName(Path root, Path path)
  2. getResourcePath(Class resourceClass, String resourceName)
  3. getResourceRootPath(Class resourceClass, String resourceName)
  4. getRoot(final String pathName)
  5. getRootPathFromDirectory(String resourceName, URL resource)
  6. getSolrCorePath(Path solrHome, String indexName)
  7. getSourceRoot(Path filePath, String pkgName)
  8. getTestDataPath(Class clazz, String name)
  9. getUniqueFileInDirectory(Path dir, String name)