Create URL object in various ways in Java
Description
The following code shows how to create URL object in various ways.
Example
import java.net.MalformedURLException;
import java.net.URL;
/*from w ww . j ava2 s . com*/
public class Main {
public static void main (String args[]) {
URL webURL, ftpURL;
try {
webURL = new URL("http://www.macfaq.com/vendor.html");
System.out.println(webURL);
ftpURL = new URL("ftp://ftp.macfaq.com/pub/");
System.out.println(ftpURL);
webURL = new URL("http", "www.macfaq.com", "/vendor.html");
System.out.println(webURL);
ftpURL = new URL("ftp", "ftp.macfaq.com", "/pub");
System.out.println(ftpURL);
webURL = new URL("http", "www.macfaq.com", 80, "/vendor.html");
System.out.println(webURL);
ftpURL = new URL("ftp", "ftp.macfaq.com", 21, "/pub");
System.out.println(ftpURL);
}
catch (MalformedURLException e) {
System.err.println(e);
}
}
}
The code above generates the following result.