c:\ece538\ch15\SerializationApps>java CreateSequentialFile Enter account number, first name, last name and balance. Enter end-of-file indicator to end input. ? 1000 Alice White 500 ? 1001 Peter Green -100 ? 1002 Sally Pink 600 ? ^Z |
See also Account.java
CreateSequentialFile.java
// Fig. 15.10: CreateSequentialFile.java // Writing objects sequentially to a file with class ObjectOutputStream. import java.io.IOException; import java.io.ObjectOutputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.util.NoSuchElementException; import java.util.Scanner; public class CreateSequentialFile { private static ObjectOutputStream output; // outputs data to file public static void main(String[] args) { String filename = args.length>0? args[0]: "clients.ser"; openFile(filename); addRecords(); closeFile(); } public static void openFile(String filename) { try { output = new ObjectOutputStream( Files.newOutputStream(Paths.get(filename))); } catch (IOException ioException) { System.err.println("Error opening file. Terminating."); System.exit(1); // terminate the program } } // add records to file public static void addRecords() { Scanner input = new Scanner(System.in); System.out.printf("%s%n%s%n? ", "Enter account number, first name, last name and balance.", "Enter end-of-file indicator to end input."); while (input.hasNext()) // loop until end-of-file indicator { try { // create new record; this example assumes valid input Account record = new Account(input.nextInt(), input.next(), input.next(), input.nextDouble()); // serialize record object into file output.writeObject(record); } catch (NoSuchElementException elementException) { System.err.println("Invalid input. Please try again."); input.nextLine(); // discard input so user can try again } catch (IOException ioException) { System.err.println("Error writing to file. Terminating."); break; } System.out.print("? "); } } // close file and terminate application public static void closeFile() { try { if (output != null) output.close(); } catch (IOException ioException) { System.err.println("Error closing file. Terminating."); } } }
Maintained by John Loomis, updated Mon Feb 03 11:39:51 2020