Java IO Tutorial - 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;
/*w w w  .  j  a v a  2  s  . co  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.