Convert a URL to a URI in Java

Description

The following code shows how to convert a URL to a URI.

Example


//  w w  w .j av  a 2s .  c  om
import java.net.URI;
import java.net.URL;

public class Main {
  public static void main(String[] argv) throws Exception {

    URI uri = null;
    URL url = null;

    // Create a URI
    uri = new URI("file://D:/1.4/Ex1.java");
    System.out.println(uri);
    url = uri.toURL();
    System.out.println(url);
    uri = new URI(url.toString());
    
    System.out.println(uri);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode