File Input and Output

import java.io.*;
import java.util.*;
import java.awt.*;

public class Bowlers extends Frame
{
   public Bowlers()
   {
      setTitle("Bowlers");
      add ("North",  titlePanel  = new Titles());
      add ("Center", scorePanel  = new Scores());
      add ("South",  actionPanel = new Actions(this));
      add ("West",   playerPanel = new Players());
   }

   public static void main(String[] args)
   {
      Frame f = new Bowlers();
      f.resize(600, 300);
      f.show();
   }

   public boolean handleEvent(Event evt)
   {
      if (evt.id == Event.WINDOW_DESTROY) System.exit(0);
      return super.handleEvent(evt);
   }

   public void addTeam(String s)
   {
      titlePanel.addTeam(s);
   }

   public void clearForm()
   {
      scorePanel.clearScores();
      playerPanel.clearPlayers();
   }

   public void loadForm()
   {
      String filename = new String("Bowl_" + replaceAll(titlePanel.getName(), " ", "_") + "_" + String.valueOf(titlePanel.getWeek()) + ".dat");
      try
      {
         DataInputStream is = new DataInputStream(new FileInputStream(filename));
         StringTokenizer t = new StringTokenizer(is.readLine(), "|");
         for (int i=0; i < 4; i++)
         {
            playerPanel.setPlayer(t.nextToken(), i);
            for (int j=0; j < 3; j++)
               scorePanel.setScore(Integer.parseInt(t.nextToken()), i, j);
         }
      }
      catch (IOException e) {  System.out.println("No Data For Selected Team");  }
   }

   public void saveForm()
   {
      String filename = new String("Bowl_" + replaceAll(titlePanel.getName(), " ", "_") + "_" + String.valueOf(titlePanel.getWeek()) + ".dat");
      try
      {
         PrintStream os = new PrintStream(new FileOutputStream(filename));
         for (int i=0; i < 4; i++)
         {
            os.print(playerPanel.getPlayer(i)); os.print("|");
            for (int j=0; j < 3; j++)
            {
               os.print(String.valueOf(scorePanel.getScore(i,j)));
               os.print("|");
            }
         }
         os.close();
      }
      catch (IOException e) { System.out.println("Cannot Open Team Score File"); }
   }

   private static String replaceAll(String s, String t, String u)
   {
      String r = "";
      int pos = 0, nextpos;

      while ((nextpos = s.indexOf(t, pos)) >= 0)
      {
         r = r + s.substring(pos, nextpos) + u;
         pos = nextpos + t.length();
      }
      r = r + s.substring(pos);
      return r;
   }

   private Scores  scorePanel;
   private Players playerPanel;
   private Actions actionPanel;
   private Titles  titlePanel;
}

class Titles extends Panel
{
   Titles()
   {
      teamnames = new String[20];
      add(new Label("  Week"));
      cb = new Choice();
      for (int i=1 ; i <= 8 ; i++) cb.addItem(String.valueOf(i));
      add(cb);
      add(new Label("Team Name"));
      team = new Choice();
      add(team);
      add(new Label("                               "));
      try
      {
         DataInputStream is = new DataInputStream(new FileInputStream("teamname.dat"));
         StringTokenizer t = new StringTokenizer(is.readLine(), "|");
         try
         {
            while ((s = t.nextToken()) != null) addTeam(s);
         }
         catch (NoSuchElementException e) {}
      }
      catch (IOException e) {  System.out.println("Team File Not Found");  }
   }

   public void writeToFile()
   {
      try
      {
         PrintStream os = new PrintStream(new FileOutputStream("teamname.dat"));
         for (int i=0 ; i < lastTeam ; i++)
         {
            os.print(teamnames[i]);
            os.print("|");
         }
         os.close();
      }
      catch (IOException e)
      {  System.out.println("Could Not Write To Team Name File");  }
   }

   public void addTeam(String s)
   {
      if (lastTeam >= 20) return;
      team.addItem(s);
      teamnames[lastTeam++] = s;
      writeToFile();
   }

   public int getWeek()
   {
      return Integer.parseInt(cb.getSelectedItem());
   }

   public String getName()
   {
      return team.getSelectedItem();
   }

   private Choice cb, team;
   private String s;
   private String[] teamnames;
   private int lastTeam = 0;
}

class Players extends Panel
{
   Players()
   {
      players = new TextField[4];
      setLayout(new GridLayout(6, 1, 20, 20));
      add(new Label("Bowler Names"));
      for (int i=0 ; i < 4 ; i++)
         add(players[i] = new TextField("Player " + String.valueOf(i), 20));
   }

   public String getPlayer(int i)
   {
      return players[i].getText();
   }

   public void setPlayer(String p, int i)
   {
      players[i].setText(p);
   }

   public void clearPlayers()
   {
      for (int i=0 ; i < 4 ; i++)
         players[i].setText("Player " + String.valueOf(i));
   }

   private TextField[] players;
}

class Actions extends Panel
{
   Actions(Bowlers p)
   {
      parentObject = p;
      setLayout(new GridLayout(1, 5, 10, 20));
      add(save  = new Button("Save"));
      add(load  = new Button("Load"));
      add(clear = new Button("Clear"));
      add(new Label("               New Team"));
      add(text  = new TextField("", 30));
   }

   public boolean action(Event evt, Object obj)
   {
      if (obj.equals("Save")) parentObject.saveForm();
      else
      if (obj.equals("Load")) parentObject.loadForm();
      else
      if (obj.equals("Clear")) parentObject.clearForm();
      else
      if (evt.target instanceof TextField) parentObject.addTeam(text.getText());
      else
         return super.action(evt, obj);
      return true;
   }

   private Button save, load, clear;
   private TextField text;
   private Bowlers parentObject;
}

class Scores extends Panel
{
   Scores()
   {
      scores = new TextField[4][3];
      setLayout(new GridLayout(6, 5, 20, 20));
      for (int i=0 ; i < 2 ; i++) add(new Label(""));
      add(new Label("Scores"));
      for (int i=0 ; i < 3 ; i++) add(new Label(""));
      for (int i=0 ; i < 3 ; i++) add(scores[0][i] = new TextField("0",3));
      for (int i=0 ; i < 2 ; i++) add(new Label(""));
      for (int i=0 ; i < 3 ; i++) add(scores[1][i] = new TextField("0",3));
      for (int i=0 ; i < 2 ; i++) add(new Label(""));
      for (int i=0 ; i < 3 ; i++) add(scores[2][i] = new TextField("0",3));
      for (int i=0 ; i < 2 ; i++) add(new Label(""));
      for (int i=0 ; i < 3 ; i++) add(scores[3][i] = new TextField("0",3));
      add(new Label(""));
   }

   public int checkValidNumber(int i)
   {
      return Math.max(0, Math.min(300, i));
   }

   public int getScore(int i, int j)
   {
      return checkValidNumber(Integer.parseInt(scores[i][j].getText()));
   }

   public void setScore(int s, int i, int j)
   {
      scores[i][j].setText(String.valueOf(checkValidNumber(s)));
   }

   public void clearScores()
   {
      for (int i=0; i < 4; i++)
         for (int j=0 ; j < 3; j++)
            scores[i][j].setText("0");
   }

   public void updateScores()
   {
      for (int i=0 ; i < 4 ; i++)
         for (int j=0 ; j < 3 ; j++)
            scores[i][j].setText(String.valueOf(checkValidNumber(Integer.parseInt(scores[i][j].getText()))));
   }

   public boolean action(Event evt, Object obj)
   {
      if (evt.target instanceof TextField)
      {
         updateScores();
         return true;
      }
      else
         return super.action(evt, obj);
   }

   private TextField[][] scores;
}