Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Simple files to store biscuits

Mastery aspect (SL):

File i/o

 

To use this Class you contruct a SImpleWriter Object using the constructor that takes a filename.

 

 

Then you use the writeLine method to put text lines into the file.

In our Biscuit example we generate these Strings with the toString() method.

 

 

The filename and an end-of-data marker. The filename may look different if you are not using ms Windows.

 

 

 

 

Calling the constructor

Calling the writeLine() method. Using toString()

Don't forget the special end of file String

We can't do too much if it doesn't work but a non-zero exit code will cause some error message to be output by the system (abnormal termination).

 

 

 

 

 

 

 

 

The constructor needs a filename.

 

 

 

 

 

 

 

The EOF is a special line (in this case we use "XXX").

NB: This requires the revised Biscuit2.java code which includes a method to create a Biscuit2 from a String as read from the file

 

Since we count from 0, we add 1 for humans.

SimpleWriter.java

SimpleReader.java

revised BiscuitWindow.java

( rename the file if you download it since you already have a BiscuitWindow Class!)

 

 

 

Someone was asking about a console version - here is a bare bones example which uses the IBIO console methods:

BiscuitConsole.java

NB: Mastery of files is only confidently achieved if your application can both read and write data files.

Simple Writer

I think there are any number of similar Classes available on the internet; we keep this one really, really simple:

import java.io.*;
/**
* Class to write simple text files
*
* @author Mr J
* @version 1.0
*/

public class SimpleWriter
{
   private PrintWriter outFile;

  public SimpleWriter(String fileName) throws IOException
   {
     outFile = new PrintWriter(new FileWriter(fileName));
   }
   public void writeLine(String line)
   {
     outFile.println(line);
   }
   public void close(){ outFile.close();}
}

To use the code, we would do something like this:

Add a couple of new data members to the Application Class:

private static final String FILENAME = "c:\\biscuits.txt";
private static final String EOF = "XXXX";

Add a new method to save the data (either call at the end of the Application or add a Save button):

/**
* Save the array contents to a text file
*/

private void saveToFile()
{
   try
   {
     SimpleWriter writer = new SimpleWriter(FILENAME);
     for(int x = 0; x <= lastBiscuit; x++)
     {
       writer.writeLine(biscuits[x].toString());
     }
     writer.writeLine(EOF);
     writer.close();
   }
   catch(IOException io)
   {
     messages.setText("Some error writing to file");
     System.exit(1);
   }
}

Simple Reader

To read in files we use the SimpleReader Class

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

/**
* Class to read and from a simple text file
*
* @author Mr J
* @version 1.0
*/

public class SimpleReader
{
   private BufferedReader inFile;

  public SimpleReader(String fileName) throws IOException
   {
     inFile = new BufferedReader(new FileReader(fileName));
   }
   public String readLine() throws IOException
   {
     return inFile.readLine();
   }
}

The code to use this SimpleReader might look like this:

/**
* load the array contents from text file
*/

private void readFromFile()
{
   try
   {
     SimpleReader reader = new SimpleReader(FILENAME);
     
// read first line of file, if file is empty should be EOF
     String next = reader.readLine();
     while (!next.equals(EOF))
     {
       lastBiscuit = lastBiscuit + 1;
       biscuits[lastBiscuit] = new Biscuit2(next);
       next = reader.readLine();
     }
     
// show the last one, check for none
     if (lastBiscuit >=0)
     {
       current = lastBiscuit;
       display.showBiscuit(biscuits[current]);
       messages.setText("File opened with " + lastBiscuit + 1 + " records");
     }
     else
     {
       messages.setText("The file is empty");
     }
   }
   catch(IOException io)
   {
     messages.setText("Error reading from file");
   }
}

Back to top

Exercise

Complete your database application so that it can read, write and store objects, move through the object collection.

That's it for this quick tour through database style applications.

Further ideas using text files:

Use a text file to store setup or start up options for an application. Eg an application might want to store the location of data files in a text file. It could store usernames and passwords although this is not very secure.

Use a text file to store different language options for menus and error messages. So the English version loads all the Strings into an array and outputs them using an array index. Another file holds all the translations into another language - the user chooses their language and your program simply reads the different file.

Use text files to hold data for a simulation , eg a supermarket queueing simulation. The file holds data like the number of checkouts, the number of customers, their average shopping cart load etc. The simulation can easily be run with different data sets. You could write results of the simulation into a text file.

You can output data - eg from the simulation program - as a csv file that can be read by another application such as a spreadsheet or word processor - the user can then produce graphs or tables of results if they wish. Of course, only your Java code contributes to the dossier (not, for example, any Excel macros).

Such an approach gets you out of attempting to create graphs yourself using Java libraries (a complex process which may involve a lot of work for which there is little credit for you in IB Dossier terms).

The advantage of using a text file is that all computer users pretty much know how to edit them using Notepad. Of course, this is a disadvantage too if they screw up the data file by editing and leaving it in a bad state (eg delete the EOF marker).

Related: [ Java home | Biscuit start | More on basic files ]

 

With this you have all you need to build the simplest of SL dossiers.

 


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009