Java URL is Absolute getAbsoluteUrlPathFromFile(String file)

Here you can find the source of getAbsoluteUrlPathFromFile(String file)

Description

Removes file name from URL string

License

Apache License

Parameter

Parameter Description
file the file String

Return

the URL String

Declaration

public static String getAbsoluteUrlPathFromFile(String file) 

Method Source Code


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

import java.net.URL;

public class Main {
    /**//from  w ww.ja  v a  2 s  .c o  m
     * Removes file name from URL string
     *
     * @param file the file String
     * @return the URL String
     */
    public static String getAbsoluteUrlPathFromFile(String file) {
        String url = getAbsoluteUrlFromFile(file);
        return url.substring(0, url.lastIndexOf('/'));
    }

    /**
     * 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(String file) {
        if (file == null) {
            throw new IllegalArgumentException("file must not be null");
        }

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

        return url.toString();
    }
}

Related

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