VISUALIZAR EL CONTENIDO DE UN FICHERO DESDE UN APPLET

 

 

El codigo de la pagina web puede ser este:

 

<HTML>

<HEAD>

<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">

</HEAD>

<BODY>

 

<!-- Insert HTML here -->

            <applet

                        code=Applet1.class

                        name=Applet1

                        width=320

                        height=200  VIEWASTEXT>

                       

            </applet>

 

</BODY>

</HTML>

 

y el codigo de la clase Applet1.class es:

 

 

import java.awt.*;

import java.applet.*;

import java.io.*;

import java.net.*;

 

 

/**

 * This class reads PARAM tags from its HTML host page and sets

 * the color and label properties of the applet. Program execution

 * begins with the init() method.

 */

public class Applet1 extends Applet

{

            /**

             * The entry point for the applet.

             *

             *

             */

           

           

            URL direccion;

            TextArea contenido;

            InputStream entrada;

            BufferedReader datosdeentrada;

           

            public void init()

            {

                        contenido = new TextArea(10,30);                 

                        add(contenido);

                        try

                        {

                                   direccion = new URL(getDocumentBase(), "fichero.txt");

                                  

                        }

                        catch(MalformedURLException e)

                        {

                                   contenido.setText("Imposible cargar el archivo fichero.txt");

                        }

            }

            public void start()

            {

                        String texto;

                        try

                        {

                                   entrada = direccion.openStream();

                                   datosdeentrada = new BufferedReader(new InputStreamReader(entrada));

                                   contenido.setText("El contenido del archivo fichero.txt es:"+'\n');

                        while((texto = datosdeentrada.readLine())!=null)

                                   contenido.append(texto+'\n');

                        datosdeentrada.close();

                        }

                        catch(IOException e)

                        {

                                   System.out.println(e);

                        }

            }

                        // TODO: Add any constructor code after initForm call.

 

}