Java URI to Relative URI relativeFileToURI(File file)

Here you can find the source of relativeFileToURI(File file)

Description

Returns a URI for the given file.

License

Open Source License

Parameter

Parameter Description
file the file to transform

Return

a (relative) URI for the given file

Declaration

public static URI relativeFileToURI(File file) 

Method Source Code

//package com.java2s;
/*/*w  w  w  . j  a v  a  2 s.c o  m*/
 * Copyright (c) 2012 Data Harmonisation Panel
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     HUMBOLDT EU Integrated Project #030962
 *     Data Harmonisation Panel <http://www.dhpanel.eu>
 */

import java.io.File;

import java.net.URI;

public class Main {
    /**
     * Returns a URI for the given file.
     * 
     * In contrast to {@link File#toURI()} it does not resolve a relative file,
     * but instead returns a relative URI.
     * 
     * @param file the file to transform
     * @return a (relative) URI for the given file
     */
    public static URI relativeFileToURI(File file) {
        if (file.isAbsolute())
            return file.toURI();
        else {
            String path = file.getPath();
            if (File.separatorChar != '/')
                path = path.replace(File.separatorChar, '/');
            return URI.create(path);
        }
    }
}

Related

  1. getRelativeURI(URI base, URI uri)
  2. getRelativeURI(URI repositoryXml, URI bundleJar)
  3. getRelativeUri(URI rootURI, File file)
  4. getRelativeURI(URI uri, URI relativeTo)
  5. relativeConfig(URI uri)
  6. relativePath(final URI baseURI, final URI pathURI)
  7. relativeURI(String basePath, String path)
  8. relativize(URI base, URI child)
  9. relativize(URI base, URI child)