Computer Science Example
Using Files and Frames

import java.awt.event.*;
import java.awt.*;
import java.io.*;

// This example uses a frame to display data read from a file on a panel in a window.
public class TestFilesFrames
{
     public static void main (String [] args)
     {
          FileFrame frame = new FileFrame ();
          frame.addWindowListener (new WindowCloser ());
          frame.show ();
     }
} // class TestFiles

class WindowCloser extends WindowAdapter
{
     public void windowClosing (WindowEvent e) {System.exit (0);}
} // class WindowCloser

class FileFrame extends Frame
{
     private Panel panel;
     private TextField name, age;
     private Label lblName, lblAge;

/* Constructor that creates a new panel and adds labels and boxes to it.  It then reads in the data from the file and displays it in the boxes. */
     FileFrame ()
     {
          super ("List of Scores");
          setSize (250, 300);
          panel = new Panel ();
          panel.setBackground (Color.cyan);
          name = new TextField (20);
          age = new TextField (10);
          lblName = new Label ("Name");
          lblAge = new Label ("Age");
          panel.add (lblName);
          panel.add (name);
          panel.add (lblAge);
          panel.add (age);
          add (panel);
          Person person = new Person ();
          person.readData ();
          person.displayData (name, age);
     } // constructor
} // class People
// Class that stores information about a person's name and age.
class Person
{
     private String name;
     private int age;

     // A method to read data from a file.  It uses a buffered reader.
     public void readData ()
     {
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("infile.txt")));
               name = infile.readLine ();
               age = Integer.parseInt (infile.readLine ());
               infile.close ();
          }
          catch (IOException e) { System.out.println ("I/O error");}
          catch (NumberFormatException e) { System.out.println ("Number format error.");}
     } // method readData

     public void displayData (TextField n, TextField a)
     {
          n.setText (name);
          a.setText ("" + age);
     } // method displayData
} // class Person