package testbufferedreader;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
class TestBufferedReader extends JFrame{
private File inFile;
private FileReader fileReader;
private BufferedReader bufReader;
private JTextArea j = new JTextArea();
private Container c;
public TestBufferedReader() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
// set up the container in which components can
// be placed
c = getContentPane();
// add the only component, the JTextArea
c.add(j);
}
public void readFile()throws IOException {
// Open the file
inFile = new File("c:\\animals.dat");
// Create a file stream, wrapping the file
fileReader = new FileReader(inFile);
// Create a second wrapper,
bufReader = new BufferedReader(fileReader);
// Read in the first line...
String s;
// Read in 7 lines (how can this be improved?)
for (int i=0; i<7; i++) {
s = bufReader.readLine();
// Add the new string to the text area
j.append(s);
j.append("\n"); // Append a new line character to improve formatting
}
}
public static void main(String args[]) throws IOException {
System.out.println("Starting TestBufferedReader...");
TestBufferedReader mainFrame = new TestBufferedReader();
mainFrame.setSize(400, 400);
mainFrame.setTitle("TestBufferedReader");
mainFrame.setVisible(true);
mainFrame.readFile();
}
}
|