Convert URI to URL
In this chapter you will learn:
Convert URI to URL with toURL()
import java.net.URI;
import java.net.URL;
/* j a v a 2 s . c o m*/
public class Main {
public static void main(String[] args) throws Exception {
URI uri = new URI("http://java2s.com/");
URL url = uri.toURL();
System.out.println(url);
}
}
The code above generates the following result.
The following creates a file and gets URI
from the file object.
Then it creates another file from the URL
converted from the URI
.
import java.io.File;
import java.io.InputStream;
import java.net.URI;
//j a va 2 s . c o m
public class Main {
public static void main(String[] argv) throws Exception {
File file = new File("filename");
URI uri = file.toURI();
file = new File(uri.toURL().getFile());
InputStream is = uri.toURL().openStream();
is.close();
}
}
Next chapter...
What you will learn in the next chapter:
- Get IP address for local host
- Get Host name from InetAddress
- Getting the IP Address of a Hostname
- Get all IP addresses
- Compare two InetAddresses
Home » Java Tutorial » URL URI