Java Utililty Methods Path to URL

List of utility methods to do Path to URL

Description

The list of methods to do Path to URL are organized into topic(s).

Method

URLgetWorkspaceURL(String subpath)
Create a URL pointing to a file in the application's workspace storage area.
try {
    URL url = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().toURI().toURL();
    return new URL(url, subpath);
} catch (IOException e) {
    throw new IllegalStateException(e);
StringpathAsUrlString(String path)
Convert separators in a filesystem path to URL separators.
if (!File.separator.equals(URL_SEPARATOR)) {
    path = path.replaceAll(Pattern.quote(File.separator), Matcher.quoteReplacement(URL_SEPARATOR));
return path;
StringpathFromURL(final URL url)
Returns the path to a file without the filename.
checkNotNull(url);
checkArgument(url.getFile().contains(System.getProperty("file.separator")),
        "Path does not contain a file separator.");
return url.getFile().substring(0, url.getFile().lastIndexOf("/") + 1);
booleanpathsMatch(URL realm, URL returnTo)
Check if the path of the return-to URL matches, or is a sub-directory of, the realm's path.
String realmPath = realm.getPath();
if (!realmPath.endsWith("/")) {
    realmPath += "/";
String returnToPath = returnTo.getPath();
if (!returnToPath.endsWith("/")) {
    returnToPath += "/";
return returnToPath.startsWith(realmPath);
URL[]pathsToURLs(ClassLoader classLoader, String... paths)
paths To UR Ls
if (paths != null) {
    List<URL> urls = new ArrayList<>();
    for (String path : paths) {
        urls.add(classLoader.getResource(path + PATH_DELIMITER));
    return urls.toArray(new URL[urls.size()]);
return new URL[0];
...
URLpathToUrl(String path)
path To Url
try {
    File file = new File(path);
    return file.toURI().toURL();
} catch (MalformedURLException e) {
    throw new IllegalArgumentException(
            String.format("Cannot convert %s to a an URL: %s", path, e.getMessage()), e);
URLpathToURL(String path)
path To URL
URL retval = null;
if (path == null) {
    return null;
if (!path.startsWith(java.io.File.separator) && (path.indexOf(':') != 1)) {
    path = System.getProperties().getProperty("user.dir") + '/' + path;
path = path.replace(java.io.File.separatorChar, '/');
...
URL[]pathToURLs(String path)
Converts path to array of urls.
StringTokenizer tokenString = new StringTokenizer(path);
URL[] urls = new URL[tokenString.countTokens()];
int index = 0;
while (tokenString.hasMoreTokens()) {
    urls[index++] = new URL(tokenString.nextToken());
return urls;
URL[]pathToURLs(String path)
path To UR Ls
StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
URL[] urls = new URL[st.countTokens()];
int count = 0;
while (st.hasMoreTokens()) {
    File file = new File(st.nextToken());
    URL url = null;
    url = file.toURI().toURL();
    if (url != null) {
...
URLtoTargetURL(String path)
to Target URL
return getBaseURI().resolve("target/" + path).toURL();