Java File.toURL()

Syntax

File.toURL() has the following syntax.

@Deprecated public URL toURL()  throws MalformedURLException

Example

In the following code shows how to use File.toURL() method.


import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
/*from   w w w  . ja  v  a 2s . c  o  m*/
public class Main {
  public static void main(String[] args) {
    File f = new File("c:/a/b/a/test.txt");

    URL uri;
    try {
      uri = f.toURL();
      System.out.println("uri: " + uri);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
  }
}

The code above generates the following result.