Net Url
# Java Example - Parsing URLs
[ Java Example](#)
The following example demonstrates how to use methods like `url.getProtocol()`, `url.getFile()`, etc., from the `net.URL` class to parse a URL address:
## Main.java File
import java.net.URL; public class Main{public static void main(String[]args)throws Exception{URL url = new URL(""); System.out.println("URL is " + url.toString()); System.out.println("Protocol is " + url.getProtocol()); System.out.println("File name is " + url.getFile()); System.out.println("Host is " + url.getHost()); System.out.println("Path is " + url.getPath()); System.out.println("Port is " + url.getPort()); System.out.println("Default port is " + url.getDefaultPort()); }}
The output of the above code is:
URL is is http File name is /html/html-tutorial.html Host is www..com Path is /html/html-tutorial.html Port is -1Default port is 80
[ Java Example](#)
YouTip