Java URI to Local Name getName(URI uri)

Here you can find the source of getName(URI uri)

Description

Derives name from uri.

License

Open Source License

Parameter

Parameter Description
uri whose path to get name of.

Return

name of file or directory.

Declaration

public static String getName(URI uri) 

Method Source Code


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

public class Main {
    /**/*from   w ww  . jav a  2 s  .c o  m*/
     * Derives name from uri. If the uri is local, this is done through the File
     * interface; else calls getURIName.
     * 
     * @param uri
     *            whose path to get name of.
     * @return name of file or directory.
     */
    public static String getName(URI uri) {
        if (uri == null)
            return null;
        if ("file".equals(uri.getScheme()))
            return new File(uri).getName();
        return getURIName(uri);
    }

    /**
     * Path is parsed using '/' as separator.
     * 
     * @param forwardSlashPath
     *            whose path to get name of.
     * @return name of file or directory.
     */
    public static String getURIName(String forwardSlashPath) {
        if (forwardSlashPath == null)
            return null;
        int end = forwardSlashPath.length();
        if (end == 0)
            return "";
        if (forwardSlashPath.charAt(end - 1) == '/')
            end = end - 1;
        if (end == 0)
            return "/";
        int index = forwardSlashPath.substring(0, end).lastIndexOf('/');
        return forwardSlashPath.substring(index + 1, end);
    }

    /**
     * Derives name from uri; calls overloaded method. the uri path is parsed
     * using '/' as separator.
     * 
     * @param uri
     *            whose path to get name of.
     * @return name of file or directory.
     */
    public static String getURIName(URI uri) {
        if (uri == null)
            return null;
        return getURIName(uri.getPath());
    }
}

Related

  1. getLocalName(URI uri)
  2. getLocalPart(String uri)
  3. getName(URI uri)
  4. getName(URI uri)
  5. getName(URI uri)
  6. getName(URI uri)
  7. getName(URI uri)
  8. getNameSpace(URI uri)
  9. getNameSpace(URI uri)