Java URI to Base getBase(final URI uri)

Here you can find the source of getBase(final URI uri)

Description

Get the base name of a URI (e.g.

License

Open Source License

Parameter

Parameter Description
uri The URI from which to get the base.

Return

The base file name

Declaration

public static String getBase(final URI uri) 

Method Source Code

//package com.java2s;
/*/*from  w w w . j  a  v  a  2s  .c om*/
 * Copyright (c) 2014 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial concept and implementation
 */

import java.net.URI;

import java.util.StringTokenizer;

public class Main {
    /**
     * Get the base name of a URI (e.g. no path or extension)
     *
     * <p>Returns "index" from "http:\\projects\src\index.html".</p>
     *
     * @param uri The URI from which to get the base.
     *
     * @return The base file name
     */
    public static String getBase(final URI uri) {
        if (uri != null) {
            String tempName = new String("");
            final StringTokenizer stk1 = new StringTokenizer(uri.toString(), "/\\");

            // Cruise through the string and eat up all the tokens before the last
            // directory delimiter
            while (stk1.hasMoreTokens()) {
                tempName = stk1.nextToken();
            }

            final StringTokenizer stk = new StringTokenizer(tempName, ".");
            return stk.nextToken();
        }
        return null;
    }
}

Related

  1. getBaseName(URI uri)
  2. getBaseURI()
  3. getBaseURI()
  4. getBaseURI(final URI uri)