Converting Between a URL and a URI - Java Network

Java examples for Network:URL

Description

Converting Between a URL and a URI

Demo Code

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
  public static void main(String[] args) {
    URI uri = null;/*from   www  .  j  a  va2  s  . c om*/
    URL url = null;

    // Create a URI
    try {
      uri = new URI("file://D:/folder/Ex1.java");
    } catch (URISyntaxException e) {
    }

    // Convert an absolute URI to a URL
    try {
      url = uri.toURL();
    } catch (IllegalArgumentException e) {
      // URI was not absolute
    } catch (MalformedURLException e) {
    }

    // Convert a URL to a URI
    try {
      uri = new URI(url.toString());
    } catch (URISyntaxException e) {
    }
  }
}

Related Tutorials