1  import java.awt.*;
  2  import java.io.*;
  3  import java.net.*;
  4  
  5  public class ReadText extends java.applet.Applet implements Runnable {
  6  
  7    URL url;
  8    Thread runner;
  9    TextArea ta = new TextArea("Read Text...");
 10  
 11    public void init() {
 12      try { 
 13        url = new URL(getCodeBase(), "ReadText.java"); 
 14      } catch ( MalformedURLException e) {
 15        System.out.println("Bad URL: " + url);
 16      }
 17      setLayout(new BorderLayout());
 18      ta.setFont(new Font("TimesRoman", Font.BOLD, 18));
 19      add("Center", ta);
 20    }
 21  
 22    public void start() {
 23      if (runner == null) {
 24        runner = new Thread(this);
 25        runner.start();
 26      }
 27    }
 28  
 29    public void stop() {
 30      if (runner != null) {
 31        runner.stop();
 32        runner = null;
 33      }
 34    }
 35  
 36    public void run() {
 37      URLConnection conn = null;
 38      DataInputStream data = null;
 39      String line;
 40      StringBuffer buf = new StringBuffer();
 41      try { 
 42        conn = url.openConnection();
 43        conn.connect();
 44        ta.setText("Connection opened...");
 45        data = new DataInputStream(new BufferedInputStream(conn.getInputStream()));
 46        ta.setText("Reading data...");
 47        while ((line = data.readLine()) != null)
 48          buf.append(line + "\n");
 49        ta.setText(buf.toString());
 50      } catch (IOException e) {
 51        System.out.println("IO Error:" + e.getMessage());
 52      }
 53    }
 54    
 55  } // end ReadText class
 56  
 57        
 58