Java URL is Absolute getAbsoluteUrlFromFile(final String file)

Here you can find the source of getAbsoluteUrlFromFile(final String file)

Description

Converts a file String to a valid URL String.<br> Example: <code>index.html</code> converts to <code>file://C:/path/to/file/index.html</code>.

License

Apache License

Parameter

Parameter Description
file the file String

Return

the URL String

Declaration

public static String getAbsoluteUrlFromFile(final String file) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.net.URL;

public class Main {
    /**// w  w  w  .ja  v  a 2  s  .  c  om
     * Converts a file String to a valid URL String.<br>
     * Example: <code>index.html</code> converts to <code>file://C:/path/to/file/index.html</code>.
     *
     * @param file the file String
     * @return the URL String
     */
    public static String getAbsoluteUrlFromFile(final String file) {
        if (file == null) {
            throw new IllegalArgumentException("file must not be null");
        }

        final URL url = ClassLoader.getSystemResource(file);
        if (url == null) {
            throw new NullPointerException("url from file=" + file + " is null");
        }

        return url.toString();
    }
}

Related

  1. getAbsoluteURL(String baseUrl, String url)
  2. getAbsoluteURL(String baseURLString, String relURlString)
  3. getAbsoluteURL(String relativeURL, String baseURL)
  4. getAbsoluteURL(String url, URL base)
  5. getAbsoluteUrl(URL baseUrl, String relativeUrl)
  6. getAbsoluteUrlPathFromFile(String file)
  7. isAbsolute(String uri)
  8. isAbsolute(String uri)
  9. isAbsolute(String uri)