/*****************************
  TEXT FILES
  
This program demonstrates reading and writing a sequential text-file.
See notes below for more details.

*******************************/
import java.awt.*;     
import java.io.*;              // needed for file classes and methods

public class FileDemo extends EasyApp
{  public static void main(String[] args)
   {  new FileDemo(); }
   
   TextField tFileName = addTextField("info.txt",30,30,150,30,this);
   Button bInput = addButton("Input Data",30,60,150,30,this);
   Button bSave = addButton("Save",30,90,75,30,this);
   Button bLoad = addButton("Load",105,90,75,30,this);
   List lInfo = addList("this|is|a|list|of|data",30,120,150,150,this);
   Button bClear = addButton("Clear",30,270,150,30,this);
   
   public FileDemo()
   {  this.setSize(200,350);
      this.setTitle("FileDemo");
   }

   public void actions(Object source,String command)
   {  if (source == bInput)
      {  doInputData();  }
      else if (source == bSave)
      {  doSaveFile();  }
      else if (source == bLoad)
      {  doLoadFile();  }
      else if (source == bClear)
      {  lInfo.removeAll(); }
   }
   
   void doInputData()
   {  String data = input("Type some data:");   // input new data
      lInfo.add(data);                          // add data to list
   }
   
   void doSaveFile()
   {  try
      {  String filename = tFileName.getText();         // get file name
         PrintWriter file = new PrintWriter(new FileWriter(filename));  
                                                        // open file for writing
         for (int x = 0; x < lInfo.getItemCount(); x=x+1)  // count thru list
         {  String data = lInfo.getItem(x);                // get data pos x
            file.println( data );                          // write data to file
         }
         file.close();                               // finished, close file
      }
      catch(IOException e)                          // handle file access errors
      {  output("Error " + e + " while writing file"); }
   }
   
   void doLoadFile()
   {  try
      {  lInfo.removeAll();                         // empty list box
         String filename = tFileName.getText();     // get file name
         BufferedReader file = new BufferedReader(new FileReader(filename));
                                                    // open file for reading
         while(file.ready())                        // loop untile end of file
         {  String data = file.readLine();          // read line of data
            lInfo.add(data);                        // add to list box
         }
         file.close();
      }
      catch(IOException e)                        // handle file access errors
      {  output("Error " + e + " while writing file"); }
   }
}
/*********************
  Sequential Text Files

A Text File contains simple text - no formatting - like you would write in Notepad.
There are HARD CARRIAGE RETURNS at the end of each line.  That means a word-processor
will not "wrap" the text, but leave it on the lines where it is.

SEQUENTIAL means the file is written (and read) in order,
from the beginning to the end.  This is the simplest way for a computer to
access a data file - start reading at the beginning, stop reading at the end.

The PLAN for writing a text file looks like this:

   Write Text File
   --------------------
      Create an empty file, with an appropriate name
      Connect to the file
      Write one line of text into the file
      Write another line of text into the file
      Write another line...
      Keep writing lines of text until you are finished
      Close the file (break the connection)
      
The PLAN for reading a text file looks like this:
      
   Read Text File
   ----------------
      Open a connection to the file
      Read one line of text
      Read another line of text
      Read another line of text
      ...
      Stop reading when you get to the end of the file
      Close the file (break the connection)

These plans only talk about DATA TRANSFER between the program and the file.
They don't mention the USEFUL WORK that might be done.  For example,
a program might read a text-file to SEARCH for a specific entry, or to COUNT
how many times something appears in the file, or to ADD UP some numbers,
or display a picture. It might write into a text-file to SAVE information
about what was happening in the program, or to TRANSFER information to be used
by another program or to create a file in a specific structure (e.g. HTML).  

The FileDemo program writes the contents of a List-box into a file,
or reads the contents of the file into the same List-box.  It doesn't
do much useful work during this transfer.  It is only a demonstration.
***********************/