使用ObjectOutputStream类将对象顺序写入文件。
import java.io.IOException; import java.io.ObjectOutputStream;/**来自 时代Java公众号 - nowjava.com**/ import java.nio.file.Files; import java.nio.file.Paths; import java.util.NoSuchElementException; import java.util.Scanner; public class Main { private static ObjectOutputStream output; // outputs data to file public static void main(String[] args) { openFile(); addRecords(); closeFile(); } // open file clients.ser /* 时代Java公众号 - nowjava.com */ public static void openFile() { try { output = new ObjectOutputStream( Files.newOutputStream(Paths.get("clients.ser"))); } 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."); } } } class Account implements java.io.Serializable { private int account; private String firstName; private String lastName; private double balance; // initializes an Account with default values public Account() { this(0, "", "", 0.0); // call other constructor } // initializes an Account with provided values public Account(int account, String firstName, String lastName, double balance) { this.account = account; this.firstName = firstName; this.lastName = lastName; this.balance = balance; } // set account number public void setAccount(int acct) { this.account = account; } // get account number public int getAccount() { return account; } // set first name public void setFirstName(String firstName) { this.firstName = firstName; }