ユーザー入力に基づいて属性が割り当てられたオブジェクトがいくつかあります。次に、これらのオブジェクトをベクターに保存し、そのベクターをファイルに書き込みますが、保存されたベクターを逆シリアル化すると、最初のオブジェクトのみが読み取られます。これが私がこれまでに持っているコードです:
public Vector<Cases> registerCase() {
Vector<Cases> casesVector = new Vector<Cases>(10, 2);
Scanner myCase = new Scanner(System.in);
Scanner userChoice = new Scanner(System.in);
System.out.println("HOW MANY CASES DO YOU WANT TO REGISTER?");
int count = userChoice.nextInt();
for (int i = 0; i < count; i++) {
Cases newCase = new Cases();
System.out.println("Enter the case name: ");
newCase.caseName = myCase.nextLine();
System.out.println("Enter the client's name: ");
newCase.client = myCase.nextLine();
System.out.println("Enter the case type: ");
newCase.caseType = myCase.nextLine();
if((newCase.caseType.equals("Major")) || newCase.caseType.equals("major")){
newCase.closedCaseRevenue = majorCasePrice;
}else {
newCase.closedCaseRevenue = (int) (0.75 * majorCasePrice);
}
casesVector.add(newCase);
}
try{
// Open a file to write to, named SavedCases.sav.
FileOutputStream saveFile = new FileOutputStream("SavedCases.sav", true);
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(casesVector);
save.close();
}
catch(Exception exc){
exc.printStackTrace();
}
Vector<Cases> comVector = new Vector<Cases>();
try{
FileInputStream saveFile = new FileInputStream("SavedCases.sav");
ObjectInputStream save = new ObjectInputStream(saveFile);
comVector = (Vector<Cases>) save.readObject();
System.out.println("The size of the vector is: " + comVector.size());
save.close(); // This also closes saveFile.
}
catch(Exception exc){
exc.printStackTrace();
}
for (Cases law_case : comVector) {
System.out.println("Name: " + law_case.caseName);
System.out.println("Client: " + law_case.client);
System.out.println("Case Type: " + law_case.caseType);
System.out.println("Case State: " + law_case.caseState);
System.out.println("Case Revenue: " + law_case.closedCaseRevenue);
System.out.println();
}
return casesVector;
}