Here you can find the source of fileToURL(File file)
public static URL fileToURL(File file) throws UnsupportedEncodingException, MalformedURLException
//package com.java2s; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; public class Main { public static URL fileToURL(File file) throws UnsupportedEncodingException, MalformedURLException { String path = file.getAbsolutePath(); path = URLEncoder.encode(path, "UTF8"); if (!path.startsWith("/")) { path = "/" + path; }// w w w.java2 s .c om if (!path.endsWith("/") && file.isDirectory()) { path = path + "/"; } URL url = new URL("file", "", path); return url; } }