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 file)
file To URL
String path = file.getAbsolutePath();
path = URLEncoder.encode(path, "UTF8");
if (!path.startsWith("/")) {
    path = "/" + path;
if (!path.endsWith("/") && file.isDirectory()) {
    path = path + "/";
URL url = new URL("file", "", path);
return url;
URL[]fileToURL(File... files)
file To URL
URL[] urls = new URL[files.length];
int i = 0;
for (File file : files) {
    urls[i] = file.toURI().toURL();
    i++;
return urls;
URLfileToURL(String file)
file To URL
File f = new File(file);
return f.toURI().toURL();
StringfileToURL(String filename)
file To URL
File f = new File(filename);
try {
    return f.toURI().toURL().toString();
} catch (MalformedURLException e) {
    return filename;
URLfileToURL(String filename)
file To URL
try {
    return new File(filename).toURI().toURL();
} catch (MalformedURLException e) {
    e.printStackTrace();
    return null;
StringfileToURLString(File file)
file To URL String
return file.toURI().toURL().toString();