Java Utililty Methods File to URL

List of utility methods to do File to URL

Description

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

Method

URLfileToURL(File f)
This method is added because the default conversion using file.toURL() turns out to be rather slow, as it tries to figure out if the file is actually a directory.
String absPath = f.getAbsolutePath();
    char sep = File.separatorChar;
    if (sep != '/') {
        absPath = absPath.replace(sep, '/');
if (absPath.length() > 0 && absPath.charAt(0) != '/') {
...
URLfileToURL(File file)
file To URL
return new URL("file", "", filenameToURI(file.getCanonicalPath()));
URLfileToURL(File file)
Returns a URL corresponding to the specified File or null if the File cannot be converted into a URL.
try {
    String path = file.getAbsolutePath();
    if (File.separatorChar != '/')
        path = path.replace(File.separatorChar, '/');
    if (!path.startsWith("/"))
        path = "/" + path;
    if (!path.endsWith("/") && file.isDirectory())
        path = path + "/";
...
URLfileToUrl(File file)
file To Url
return new URL("file:///" + file.getCanonicalPath().replaceAll("\\\\", "/"));
URLFileToURL(File file)
File To URL
return file.toURI().toURL();
URLfileToURL(File file)
Converts a File to a file: URL.
try {
    file = file.getCanonicalFile();
} catch (IOException ignored) {
    file = file.getAbsoluteFile();
URL url;
try { 
    String path = file.getPath().replace('\\', '/');
...
URLfileToURL(File file)
file To URL
file = file.getCanonicalFile();
String s = file.getAbsolutePath();
s = s.replace('\\', '/');
if (!s.startsWith("/"))
    s = "/" + s;
if (!s.endsWith("/") && file.isDirectory())
    s = s + "/";
return new URL("file", "", s);
...
URLfileToURL(File file)
A replacement for File.toURI().toURL().
try {
    URL url = file.toURI().toURL();
    String string = url.toExternalForm();
    if (string.contains("+")) {
        string = string.replace("+", "%2B");
    if (string.contains(" ")) {
        string = string.replace(" ", "%20");
...
URLfileToURL(File file)
file To URL
try {
    return file.toURI().toURL();
} catch (MalformedURLException e) {
    try {
        return file.toURI().toURL();
    } catch (MalformedURLException e1) {
        try {
            return new URL("file:/" + file.getCanonicalPath().replace('\\', '/')); 
...
URLfileToURL(File file)
file To URL
try {
    return file.toURI().toURL();
} catch (MalformedURLException e) {
    throw new RuntimeException("Unexpected exception on file [" + file + "]", e);