|
|
||
| You are here: | ||
|
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. 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: 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.*; public SimpleWriter(String fileName) throws IOException 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"; Add a new method to save the data (either call at the end of the Application or add a Save button): /** Simple Reader To read in files we use the SimpleReader Class import java.awt.*; public SimpleReader(String fileName) throws IOException The code to use this SimpleReader might look like this: /** 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.
|
|
|
|||
|
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. 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 |