import java.awt.*; import java.awt.event.*; import java.io.*; public class FileRead extends MainFrame { Button open = new Button("Open"); TextArea ta = new TextArea(); public FileRead() { super("FileInputStream Demo"); setLayout(new BorderLayout(5, 5)); add(open, "South"); add(ta, "Center"); ta.setFont(new Font("Courier", Font.PLAIN, 12)); open.addActionListener(new Opener()); show(); } class Opener implements ActionListener { public void actionPerformed(ActionEvent ae) { FileDialog fd; fd = new FileDialog(MainFrame.this, "Open", FileDialog.LOAD); fd.setFile("*.html"); fd.show(); String fname = fd.getFile(); String dname = fd.getDirectory(); if (fname == null) { ta.append("File selection cancelled\n"); } else { FileInputStream fis = null; try { fis = new FileInputStream(dname + fname); /* String text = ""; int ch; while ((ch = fis.read()) != -1) text += (char) ch; */ byte[] b = new byte[fis.available()]; fis.read(b); String text = new String(b); ta.setText(text); ta.setCaretPosition(0); } catch (FileNotFoundException nf) { ta.append("Couldn't open " + dname + fname); } catch (IOException ioe) { ta.append("Error reading file"); } finally { try { if (fis != null) fis.close(); } catch (IOException ie) { } } } } } public static void main(String[] args) { new FileRead(); } }