import java.io.*; /** * This program will read a sequential file ("javaapp.dat") which * was created by WriteFile.java * @author Ron Zucker; modified slightly by Bob Roggio */ public class ReadFile { /** * This is an application that reads data from a sequential file * and outputs the data to the screen * @param String containing name of file to be read */ public static void main(String[] args) throws IOException { String outString; BufferedReader input; DataOutputStream output; /* this code assigns the file javaapp.dat to a datastream which is roughly equivalent to an open... */ input= new BufferedReader(new FileReader("javaapp.dat")); /* this is the loop to continue until an eof is found note: that this is the equivalent of a priming and a loop read The file is read and the data read is placed in outstring As long as outstring is not null the loop will continue! */ while ((outString=getData(input))!=null) { System.out.println("value read was:"+ outString); } // close the input file... input.close(); System.out.println("all done"); }// end main() /** * This function receives data from a sequential file * @return a string containing the data read from the file * @param infile BufferedReader with the name of file to be read */ public static String getData(BufferedReader infile) throws IOException { String readdata; try { readdata=infile.readLine(); } catch (EOFException e) { System.out.println("end of file" +e.toString()); readdata=String.valueOf(""); } return readdata; } // end getData () } // end class ReadFile