/* URL fetching code taken and modified from Java in a Nutshell page 143 */

import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;


public class SimpleIO extends Applet implements ActionListener  {

//Put any data members here

private Button startUp;



//init is the first code that will run

public void init() {

  startUp = new Button("Start");  //create a button with the label start
  this.add(startUp);              //add the button to the layout manager
  startUp.addActionListener(this);
}

//actionPerformed gets all the user input from the applet
 
/* All the code here will begin when you click on the start button.
   The output will be displayed in the Java console window */
 
       public void actionPerformed(ActionEvent event){
         try {
          doMain();
	     }
         catch(MalformedURLException me){ System.out.println(me);}
         catch(IOException e) {System.out.println(e);}
       System.out.println("This is a sample of catching IO errors. I should put my main() in the doMain() method"); 
       }

/* All the code here is called above by the call doMain() */

public void doMain() 
   throws MalformedURLException, IOException {

     /* Get the name of the input file (passed in the applet tag 
        PARAM in the index.html) and the URL */
     URL url = new URL(this.getParameter("inputFile"));

     /* Open a connection to the URL.              */
     URLConnection connection = url.openConnection();

     printinfo(connection);

}

  /* Print info will read in data from a URL */

  public static void printinfo(URLConnection u) throws IOException {

   System.out.println(u.getURL().toExternalForm()+":");
   
    BufferedReader in  = new BufferedReader(new InputStreamReader(u.getInputStream()));

   /* The inputstream 'in' is very similar to a file pointer.  You can loop
      and read in all data you need ... this one only reads 5 lines.
      You will still need to parse the lines to know what to do */

   for (int i=0;i<5;i++) {
      String line = in.readLine();
      if(line==null) break;
      System.out.println(" " + line);
   }

  }

} //applet


