Java URI Create getUri(File file)

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

Description

Returns the URI for the given file.

License

Open Source License

Parameter

Parameter Description
file The input file.

Exception

Parameter Description
MalformedURLException if an error occur while constructing the URI for the givenfile.

Return

The URI for the given file.

Declaration

public static String getUri(File file) throws MalformedURLException 

Method Source Code


//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import java.io.File;

import java.net.MalformedURLException;

public class Main {
    /**/*w  ww . j  av a 2 s .  co  m*/
     * Returns the URI for the given file.
     * 
     * @param file
     *            The input file.
     * @return The URI for the given file.
     * @throws MalformedURLException
     *             if an error occur while constructing the URI for the given
     *             file.
     */
    public static String getUri(File file) throws MalformedURLException {
        String url = file.toURL().toExternalForm();
        StringBuffer strBuf = new StringBuffer();
        int urlLength = url.length();
        for (int i = 0; i < urlLength; i++) {
            char ch = url.charAt(i);
            switch (ch) {
            case ' ':
                strBuf.append("%20"); //$NON-NLS-1$
                break;
            default:
                strBuf.append(ch);
                break;
            }
        }
        return strBuf.toString();
    }
}

Related

  1. createURI(String uri)
  2. createURI(String uri)
  3. createURI(String uri)
  4. createURIWithFull(final String scheme, final String userInfo, final String host, final int port, final String path, final String query, final String fragment)
  5. getUri()
  6. getURI(File file)
  7. getUri(File file)
  8. getURI(File file)
  9. getURI(final String address)