Here you can find the source of fileToURL(File file)
public static final URL fileToURL(File file) throws MalformedURLException, IOException
//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; } }