Java I/O How to - Get a Proper URL from a File Object








Question

We would like to know how to get a Proper URL from a File Object.

Answer

//from www.j ava2  s.co  m
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
  public static void main(String args[]) throws MalformedURLException {
    File file = new File("The End");
    URL url2 = file.toURI().toURL();
    System.out.printf("Good url %s%n", url2);
  }
}

The code above generates the following result.