LEER UN FICHERO TIPO
TEXTO
import
com.ms.wfc.app.*;
import
com.ms.wfc.core.*;
import
com.ms.wfc.ui.*;
import
com.ms.wfc.html.*;
// TODO 1: Importar
el paquete java.io
import java.io.*;
/**
* Esta clase puede tomar un nœmero variable de
par‡metros en la l’nea de
* de comandos. La ejecuci—n del programa
comienza con el mŽtodo main(). El
* constructor de clase no se invoca a menos
que se cree un objeto del tipo
* 'ReadFile' en el mŽtodo main().
*/
public class
ReadFile extends Form
{
public ReadFile()
{
super();
// Necesario para
el soporte de Visual J++ Form Designer
initForm();
}
/**
* ReadFile omite la colocaci—n, por lo que
puede limpiar la
* lista de componentes.
*/
public void dispose()
{
super.dispose();
components.dispose();
}
private void btnRead_click(Object
source, Event e)
{
// TODO 3: Abrir
el archivo
FileReader read;
try
{
read
= new FileReader(edtInput.getText());
}
catch(FileNotFoundException
fnf)
{
edtOutput.setForeColor(Color.RED);
edtOutput.setBackColor(Color.WHITE);
edtOutput.setText("***
Error al abrir: "
+
fnf.getMessage() + " ***");
return;
}
edtOutput.setForeColor(Color.WINDOWTEXT);
edtOutput.setBackColor(Color.WINDOW);
edtOutput.setText("");
// TODO 4: Matriz
que guarda los caracteres le’dos desde el archivo
char[] buffer =
new char[256];
// TODO 5: El
nœmero de caracteres le’dos
int length;
// TODO 6: Leer
el archivo en edtOutput
try
{
while((length
= read.read(buffer)) != -1)
{
String
s = new String(buffer, 0, length);
edtOutput.setText(edtOutput.getText()
+ s);
}
}
catch(IOException
io)
{
edtOutput.setForeColor(Color.RED);
edtOutput.setBackColor(Color.WHITE);
edtOutput.setText(edtOutput.getText()
+
"\n\n*** Error al leer el archivo ***");
}
// TODO 7: Cerrar
el fichero
try
{
read.close();
}
catch(IOException
io)
{
}
// EJEMPLO
Centrar la atenci—n a edtInput
edtInput.focus();
}
private void
edtOutput_textChanged(Object source, Event e)
{
}
/**
* NOTA: El siguiente c—digo es necesario para
el dise–ador de formularios
* de Visual J++. Puede modificarse editor de
formularios. No lo
* modifique utilizando el editor de c—digo.
*/
Container components = new
Container();
Edit edtInput = new Edit();
Button btnRead = new Button();
Edit edtOutput = new Edit();
private void initForm()
{
this.setText("ReadFile");
this.setAutoScaleBaseSize(new
Point(5, 13));
this.setClientSize(new
Point(199, 305));
edtInput.setAnchor(ControlAnchor.TOPLEFTRIGHT);
edtInput.setLocation(new
Point(8, 8));
edtInput.setSize(new
Point(100, 20));
edtInput.setTabIndex(0);
edtInput.setText("c:\\autoexec.bat");
btnRead.setAnchor(ControlAnchor.TOPRIGHT);
btnRead.setLocation(new
Point(112, 8));
btnRead.setSize(new
Point(75, 23));
btnRead.setTabIndex(1);
btnRead.setText("Read");
btnRead.addOnClick(new
EventHandler(this.btnRead_click));
edtOutput.setAnchor(ControlAnchor.ALL);
edtOutput.setEnabled(false);
edtOutput.setLocation(new
Point(0, 40));
edtOutput.setSize(new
Point(200, 264));
edtOutput.setTabIndex(2);
edtOutput.setText("");
edtOutput.setMultiline(true);
this.setNewControls(new
Control[] {
edtOutput,
btnRead,
edtInput});
}
/**
* Punto de entrada principal para la
aplicaci—n.
*
* @param args Matriz de par‡metros pasados a
la aplicaci—n
* mediante la l’nea de comandos.
*/
public static void main(String
args[])
{
Application.run(new
ReadFile());
}
}