Importing Classes

 

Fully qualifed names like java.net.URL are not the most convenient thing to have to type. You can use the shorter class names like URL without the java.net part if you first import the class by adding an import statement at the top of the file. For example,
import java.net.URL;
import java.net.MalformedURLException;

public class URLSplitter {

  public static void main(String[] args) {
   
    for (int i = 0; i < args.length; i++) {
      try {
        URL u = new URL(args[i]);
        System.out.println("Protocol: " + u.getProtocol());
        System.out.println("Host: " + u.getHost());
        System.out.println("Port: " + u.getPort());
        System.out.println("File: " + u.getFile());
        System.out.println("Ref: " + u.getRef());
      }
      catch (MalformedURLException e) {
        System.err.println(args[i] + " is not a valid URL");
      }
    }
       
  }
}

List | Previous | Next