The Java Program: WWWGet.java

  1 // WWWGet.java -- given a URL, copy it from the server
  2 
  3 import java.net.URL;
  4 import java.net.MalformedURLException;
  5 import java.io.BufferedReader;
  6 import java.io.InputStreamReader;
  7 import java.io.IOException;
  8 
  9 public class WWWGet {
 10 
 11    public static void main (String args[]) {
 12 
 13       if  (args.length > 0) {
 14          try {
 15             final URL u = new URL(args[0]);
 16             final BufferedReader in =
 17                new BufferedReader (new InputStreamReader (u.openStream()));
 18             String line;
 19             while ((line = in.readLine()) != null) {
 20                System.out.println (line);
 21             }
 22          } catch (MalformedURLException e) {
 23             System.err.println (args[0] + " is not a valid URL");
 24          } catch (IOException e) {
 25             e.printStackTrace (System.err);
 26          }
 27       } else {
 28          System.err.println ("The program requires a URL as an argument.");
 29       }
 30    }
 31 
 32 }