home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch10 / GetInetFile.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  2.0 KB  |  79 lines

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.net.*;
  5. import java.io.*;
  6.  
  7. public class GetInetFile extends Frame implements Runnable
  8. {
  9.     Thread thread1;
  10.     URL doc;
  11.     TextArea body = new TextArea("Getting text...");
  12.  
  13.     public GetInetFile()
  14.     {
  15.        super("Get URL");
  16.        add(body);
  17.        try
  18.        {
  19.             doc = new URL("http://www.lads.com/ads/launch.html");
  20. }//try
  21.        catch (MalformedURLException e)
  22.        {
  23.             System.out.println("Bad URL:  " + doc);
  24.        }//catch
  25.  
  26.     }//GetInetFile
  27.  
  28.  
  29.     public static void main(String[] arguments)
  30.     {
  31.        GetInetFile frame1 = new GetInetFile();
  32.  
  33.        WindowListener l = new WindowAdapter()
  34.        {
  35.             public void windowClosing(WindowEvent e) 
  36.             {
  37.                 System.exit(0);
  38.             }//windowClosing
  39.        };//WindowListener
  40.  
  41.        frame1.addWindowListener(l);
  42.        frame1.pack();
  43.        frame1.setVisible(true);
  44.        if (frame1.thread1 == null)
  45.        {
  46.           frame1.thread1 = new Thread(frame1);
  47.           frame1.thread1.start();
  48.        }//null
  49.     }//main
  50.  
  51.     public void run()
  52.     {
  53.        URLConnection URLConn = null;
  54.        InputStreamReader inStream;
  55.        BufferedReader info;
  56.        String line1;
  57.        StringBuffer strBuffer = new StringBuffer();
  58.        try
  59.        {
  60.           URLConn = this.doc.openConnection();
  61.           URLConn.connect();
  62.           body.setText("Connection opened ... ");
  63.           inStream = new InputStreamReader(URLConn.getInputStream());
  64. info = new BufferedReader(inStream);
  65.           body.setText("Reading data ...");
  66.           while ((line1 = info.readLine()) != null)
  67.           {
  68.                 strBuffer.append(line1 + "\n");
  69.           }//while
  70.           body.setText(strBuffer.toString());
  71.        }//try
  72.        catch (IOException e)
  73.        {
  74.           System.out.println("IO Error:" + e.getMessage());
  75.        }//catch
  76.     }//run
  77. }// class Getfile
  78.  
  79.