Java URI Create getURIFromPath(String fileOrURI)

Here you can find the source of getURIFromPath(String fileOrURI)

Description

Get a URI from a string.

License

Open Source License

Parameter

Parameter Description
fileOrURI a string containing either a URI or a file-system path.

Return

a properly-formed URI

Declaration

public static URI getURIFromPath(String fileOrURI) 

Method Source Code

//package com.java2s;
/**//w w w . j  ava2  s .c  om
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.*;
import java.net.*;

public class Main {
    /**
     * Get a URI from a string.  In most cases, this is just the plain URI, but in the
     * case of file-paths, this may include escaping special characters.  Effectively
     * the inverse of getNeatPathFromURI().
     *
     * @param fileOrURI a string containing either a URI or a file-system path.
     * @return a properly-formed URI
     */
    public static URI getURIFromPath(String fileOrURI) {
        URI uri = null;
        try {
            uri = new URI(fileOrURI);
            if (uri.getScheme() == null) {
                uri = new File(fileOrURI).toURI();
            }
        } catch (URISyntaxException usx) {
            // This can happen if we're passed a file-name containing spaces.
            uri = new File(fileOrURI).toURI();
        }
        return uri;
    }
}

Related

  1. getUri(String uriName)
  2. getURIAddress(URI uri)
  3. getUriByEndpoint(String endpoint)
  4. getURIFilename(final String s)
  5. getURIFromEncodedString(String unencoded)
  6. getURIFromPath(String path)
  7. getURIName(String forwardSlashPath)
  8. getURIName(URI uri)
  9. getURIParameterValue(URI uri, String name, String defVal)