Java File to URL fileToURL(File file)

Here you can find the source of fileToURL(File file)

Description

file To URL

License

Open Source License

Declaration

public static final URL fileToURL(File file) throws MalformedURLException, IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

public class Main {
    public static final URL fileToURL(File file) throws MalformedURLException, IOException {
        return new URL("file", "", filenameToURI(file.getCanonicalPath()));
    }/*  ww w. j a  va  2 s .  c  om*/

    /**
     * Fixes a platform dependent filename to standard URI form.
     *
     * @param str The string to fix.
     *
     * @return Returns the fixed URI string.
     */
    public static final String filenameToURI(String str) {
        // handle platform dependent strings
        str = str.replace(java.io.File.separatorChar, '/');
        // Windows fix
        if (str.length() >= 2) {
            if (str.charAt(1) == ':') {
                char ch0 = Character.toUpperCase(str.charAt(0));
                if (ch0 >= 'A' && ch0 <= 'Z')
                    str = "/" + str;
            }
        }
        return str;
    }
}

Related

  1. fileToURL(File f)
  2. fileToURL(File file)
  3. fileToUrl(File file)
  4. FileToURL(File file)
  5. fileToURL(File file)