/* TestFile1R.java CIS 111 October 20, 2007 Demonstrates random file access. This program takes whatever the user types in and writes it to a file record-by-record. */ public class TestFile1R extends CIS111App { public static void main(String[] args) { // create record to use for RA file access Record rec = new Record(); // open file for writing RandomFile fileout = new RandomFile("files1R.dta", rec); // loop to get input from user and write to file // (notice use of priming read) int recordNumber = 0; String strIn = getString("Enter line (QUIT to exit): "); while (!strIn.equalsIgnoreCase("QUIT")) { rec.msg = strIn; recordNumber++; fileout.put(recordNumber, rec); strIn = getString("Enter line (QUIT to exit): "); } // close output file fileout.close(); // must reset rec before reopening file rec = new Record(); // now let's read back what's in the file RandomFile filein = new RandomFile("files1R.dta", rec); println("Here's what is in the file: "); // loop to read and display data from file for (int recno = 1; recno <= filein.numberOfRecords(); recno++) { rec = filein.get(recno); println(rec.msg); } // close input file filein.close(); } } class Record { String msg = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; }