import java.io.*; /** * the purpose of this program is to read data from the keyboard and put it * into a sequential file for processing by ReadFile * @author Ron Zucker; modified slightly by Bob Roggio */ public class WriteFile { /** * this is the main program which will read from the keyboard and display * the given input onto the screen and also write the data to a file * @param Array of Strings (not used in this program) */ public static void main(String[] args) throws IOException { String outString; DataOutputStream output=null; // this code assigns the standard input to a datastream BufferedReader input= new BufferedReader(new InputStreamReader(System.in)); // this code opens the output file... try { output= new DataOutputStream(new FileOutputStream("javaapp.dat")); } // end try() // handle any open errors catch (IOException e) { System.err.println("Error in opening output file\n" + e.toString()); System.exit(1); } // end catch() // priming read...... outString=getData(input); // this is the loop to continue while nnonblank data is entered while (!outString.equals("")) { System.out.println("value entered was:"+ outString); output.writeBytes(outString+"\r\n"); outString=getData(input); } // end while() // flush buffers and close file... try { output.flush(); output.close(); } // end try() catch (IOException e) { System.err.println("Error in closing output file\n" + e.toString()); } // end catch() System.out.println("all done"); } // end main() /** * this function will prompt and receive data from the keyboard * @return String containing the data read from the keyboard * @param BufferedReader that is connected to System.in (the keyboard) */ public static String getData(BufferedReader infile) throws IOException { System.out.println("enter a value"); return infile.readLine(); } // end getData() } // end class WriteFile