Here you can find the source of toUrl(final String path)
Parameter | Description |
---|---|
path | the local file path to parse into a URL |
Parameter | Description |
---|---|
IOException | if there is an error creating the URL |
public static final URL toUrl(final String path) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.IOException; import java.net.URL; public class Main { private static final String fileProtocolPrefix = "file:///"; /** Convert a local file path to a {@link URL} * @param path the local file path to parse into a URL * @return the URL corresponding to the specified file/folder path * @throws IOException if there is an error creating the URL *//*from w w w . ja va 2 s .co m*/ public static final URL toUrl(final String path) throws IOException { URL url = null; if (path.startsWith(fileProtocolPrefix)) { url = new URL(path); } else { // Convert the path to a URL try { url = new URL(path); } catch (Exception e) { // If the path cannot be directly converted to a URL // convert it to a file first and then convert it to a URL File file = new File(path).getCanonicalFile(); url = file.toURI().toURL(); } } return url; } }