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 URL fileToURL(File file) throws UnsupportedEncodingException, MalformedURLException 

Method Source Code

//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;
    }
}

Related

  1. fileToURL(File file)
  2. fileToURL(File file)
  3. fileToURL(File file)
  4. fileToURL(File file)
  5. fileToURL(File file)
  6. fileToURL(File... files)
  7. fileToURL(String file)
  8. fileToURL(String filename)
  9. fileToURL(String filename)