/* ReadaFile is simple file reader for JavaPlugin. M. Gallant 03/27/2002 */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.io.* ; import java.util.*; public class ReadaFile extends Applet implements ActionListener { int strings = 0; private TextArea ta = new TextArea (35, 80); Button startbutton = new Button("Display File") ; Button clearbutton = new Button("CLEAR") ; TextField filename = new TextField(25) ; public void init() { this.setLayout(new BorderLayout(10,10)); Panel p1 = new Panel() ; this.setBackground(new Color(192, 192, 192)) ; p1.add(new Label("File Name:", Label.RIGHT)) ; p1.add(filename); p1.add(startbutton); p1.add(clearbutton); startbutton.setBackground(Color.red) ; startbutton.addActionListener(this) ; clearbutton.addActionListener(this) ; add(p1, BorderLayout.NORTH) ; add(ta, BorderLayout.CENTER) ; } public void actionPerformed(ActionEvent evt) { if(evt.getSource() == clearbutton) { ta.setText(""); filename.setText(""); } if( evt.getSource() == startbutton ) { displayFile(filename.getText()); ; } } private void displayFile(String filename) { String aline= "" ; BufferedReader buff = null; ta.append("----- Reading file: " + filename + " -----\n\n") ; try { buff = new BufferedReader(new FileReader(filename) ); while( (aline=buff.readLine()) != null) ta.append(aline + "\n"); } catch(Exception ioe) { ta.append("Problem opening or reading file\n" + ioe+ "\n") ; } finally { if(buff !=null) try { buff.close() ; ta.append("\n ---- FileReader closed ----\n") ; } catch (Exception ioc) {;} } } }