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

/**
 * <strong>ReadFromURLAppletExample</strong> -- reading the file
 * specified by a URL.
 * From J. Kanerva "The Java FAQ"
 */
public class ReadFromURLAppletExample extends java.applet.Applet {

    TextArea messageLog = new TextArea(4, 40);

    /** Builds the applet's interface. */
    public void init() {
        setLayout(new BorderLayout());
        add("Center", messageLog);
    }

    /**
     * Called when the applet is started or when the browser
     * returns to the applet's page.
     */
    public void start() {

        URL url = getDocumentBase();
        URLConnection connection;
        String inputLine;
        BufferedReader inReader;

        /* Create the URL connection and get its input stream.
         * Wrap a DataInputStream around the input stream in order to
         * read from it one line at a time.
         *
         * Close the input stream when done.
         */
        try {
            connection = url.openConnection();
            messageLog.append("File read from URL " + url + ":\n");
            inReader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
            while (null != (inputLine = inReader.readLine())) {
                messageLog.append(inputLine + "\n");
            }
            inReader.close();
        }
        catch (IOException e) {
            System.err.println("Exception:  " + e);
        }
   }
}