序列化对象数组

我的代码无法成功序列化和存储“关节”对象数组。 我曾尝试通过不同的方法进行序列化,但只能通过数组使其起作用。 该程序具有写入,读取和执行文件的正确权限,因此我不确定为什么这些对象不会序列化为单独的文件。

Joint,JointSerializer和JointSerializerTester可以在下面找到。

写作中止; java.io.NotSerializableException:ie.lyit.bank.Joint

    package ie.lyit.bank;

//INHERITANCE-Joint IS-A account
public class Joint extends account {
   private Name nameB;// COMPOSITION-nameB is an object of class Name
   private String addressB;

   // Default Constructor for Joint objects
   // - Java will automatically call account() constructor
   public Joint(){
      // super(); // Can call super() but it will do it automatically
      this.nameB=new Name();
      this.addressB="";
   }

   // Overloaded Constructor for Joint objects
   // Takes in a name,address,balance,date of opening,overdraft amount
   //          and a nameB and addressB for Joint account objects
   // Called as follows:
   // Joint j1 = new Joint(new Name("Mr","Joe","Bloggs"),//                      "123 High Road",1000.00,//                      new Date(12,6,2012),//                      new Name("Mrs","Ann",//                      "123 High Road");
   public Joint(Name name,String address,double balance,Date dateOpened,Name nameB,String addressB){
      super(name,dateOpened);        
      this.nameB=nameB;
      this.addressB=addressB;
   }    

   // toString() method for displaying a Joint object
   // Display Joint object as 
   //         "accOUNT==>100001:Mr Joe Bloggs/Mrs Ann Bloggs    €100.00" 
   @Override
   public String toString(){
      return("accOUNT==>"+ accountNo + ":" + name + "/" + nameB + "  €" + balance);    
   }

   // Don't need to override equals()
   // account equals is sufficient

   // get() and set() methods for each Instance Variable
   public Name getNameB(){
      return nameB;
   }
   public String getaddressB(){
      return addressB;
   }

   public void setNameB(Name nameB){
      this.nameB = nameB;
   }
   public void setaddressB(String addressB){
      this.addressB = addressB;
   }
} 

    package ie.lyit.serialize;

import java.util.ArrayList;
import ie.lyit.bank.*;
import java.io.*;
import java.util.Scanner;
import java.io.Serializable;

public class JointSerializer {
    private ArrayList<Joint> accounts;

    private final String FILENAME = "accounts.ser";    
    Scanner myobj = new Scanner(System.in);
    // Default Constructor
    public JointSerializer(){
        // Construct accountList ArrayList
        accounts = new ArrayList<Joint>();
    }    

    public void add(){
        // Create a Joint object



        Joint account = new Joint();
        // Read its details
        System.out.println("Enter First,Middle and Last Name:");

        // public account(Name name,Date dateOpened){
      account.setName(new Name(myobj.next(),myobj.next(),myobj.next()));
        //System.out.println("username is: " + username); 
      System.out.println("Enter address:\n");

      account.setaddress(myobj.next());

      System.out.println("Enter Balance:\n");

      account.setBalance(myobj.nextDouble());

     // System.out.println("Enter Date:");


     //account.setaddress(new address(myobj.next(),myobj.next()));



        // And add it to the accounts ArrayList
        accounts.add(account);
    }

    public void list(){
        // for every Joint object in accounts
        for(Joint tmpJoint:accounts)
            // display it
            System.out.println(tmpJoint);
    }

    // This method will serialize the accounts ArrayList when called,// i.e. it will write it to a file called accounts.ser
    public void serializeJoints(){
        try {
            // Serialize the ArrayList...
            FileOutputStream fileStream = new FileOutputStream(FILENAME);

            ObjectOutputStream os = new ObjectOutputStream(fileStream);

            os.writeObject(accounts);
            os.close();
        }
        catch(FileNotFoundException fNFE){
            System.out.println("Cannot create file to store accounts.");
        }
        catch(IOException ioE){
            System.out.println(ioE.getMessage());
        }
    }

    // This method will deserialize the accounts ArrayList when called,// i.e. it will restore the ArrayList from the file accounts.ser
    public void deserializeJoints(){
        try {
            // Deserialize the ArrayList...
            FileInputStream fileStream = new FileInputStream(FILENAME);

            ObjectInputStream is = new ObjectInputStream(fileStream);

            accounts = (ArrayList<Joint>)is.readObject();    
            is.close();
        }
        catch(FileNotFoundException fNFE){
            System.out.println("Cannot create file to store accounts.");
        }
        catch(IOException ioE){
            System.out.println(ioE.getMessage());
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
} 


    package ie.lyit.testers;

import java.util.Scanner;

import ie.lyit.serialize.JointSerializer;

public class JointSerializerTester {

    public static void main(String[] args) {
        JointSerializer JointSerializer = new JointSerializer();
        Scanner scannerInput = new Scanner(System.in);
        int userChoice;
        boolean escape = false;
        do {
            System.out.println("Please choose an option from the following\n");
            System.out.print("[1]Create an account\n[2]View an account\n[3]List all accounts \n[4]Edit an account\n[5]Delete an account\n[6]Exit\n");
            userChoice  = scannerInput.nextInt();

            if(userChoice == 1) {
                JointSerializer.add();
            }
            else if(userChoice == 2) {
                //CODE FOR VIEWING
            }
            else if(userChoice == 3) {
                //CODE FOR LISTING
                JointSerializer.list();
            }
            else if(userChoice == 4) {
                //CODE FOR EDIT
                System.out.println("Please enter the account number you wish to delete:");

            }
            else if(userChoice == 5) {
                System.out.println("Please enter the account number you wish to delete: ");
            }
            if(userChoice == 6) {//CODE FOR EXIT
                System.out.println("Thank you for using this banking application");
                escape = true;
            }

        }while(escape != true); // its a trap
        scannerInput.close();

        // Add two records to the ArrayList
        //JointSerializer.add();
        //JointSerializer.add();

        // Write the ArrayList to File
        JointSerializer.serializeJoints();


        // Read the ArrayList from the File
        JointSerializer.deserializeJoints();

        // List all the Joints in the ArrayList
        JointSerializer.list();
    }
} 


    /**
 * Class: B.Sc. in Computing
 * Instructor: Maria Boyle
 * Description: Models an abstract superclass account
 *              Abstract because it will be used for inheritance only
 * Date: 26/09/2019
 * @author Maria Boyle
 * @version 1.0
**/
package ie.lyit.bank;
import java.util.*;
import java.io.*;

// account CAN-DO whatever is in Transactable
public abstract class account implements Transactable,Serializable {
   protected Name name;     //COMPOSITION-account HAS-A name
   protected String address;    
   protected double balance;
   protected Date dateOpened;//COMPOSITION-account HAS-A dateOpened
   protected int accountNo;

   private static int nextUniqueNumber=1000;// Next available unique 
                                               // account number      

   // Default Constructor-set Instance Variables to null
   public account(){
      this.name=new Name();
      this.address="";
      this.balance=0.0;
      this.dateOpened=new Date();
      this.accountNo=nextUniqueNumber++;
   }

   // Overloaded Constructor-set Instance Variables to initial values passed in
   public account(Name name,Date dateOpened){
      this.name=name;
      this.address=address;
      this.balance=balance;
      this.dateOpened=dateOpened;
      this.accountNo=nextUniqueNumber++;
   }

   // toString() method for displaying an account object
   // Display account object as 
   // "accOUNT 100001==>Mr Joe Bloggs         €100.00" 
   @Override
   public String toString(){
      return("accOUNT " + accountNo + "==>" + name + " \t€" + balance);
   }

    // equals() method for comparison of two account objects
    @Override
    public boolean equals(Object obj){
       account aObject;
       if (obj instanceof account)
          aObject = (account)obj;
       else
          return false;

       return this.accountNo==(aObject.accountNo);
   }

    // get() methods for each Instance Variable    
    public Name getName(){
       return name;
    }
    public String getaddress(){
       return address;
    }
    public double getBalance(){
       return balance;
    }
    public Date getDateOpened(){
       return dateOpened;
    }
    public int getNumber(){
       return accountNo;
    }

    // set() methods for each Instance Variable    
    public void setName(Name name){
       this.name=name;
    }
    public void setaddress(String address){
       this.address = address;
    }
    public void setBalance(double balance){
       this.balance = balance;
    }
    public void setDateOpened(Date dateOpened){
       this.dateOpened = dateOpened;
    }
    // You should not provide a setNumber() method because
    // accountNo is unique so you should not be able to set it

    // override methods in Transactable interface
    @Override
    public void deposit(double amount) {
        balance+=amount;
    }
    @Override
    public void withdraw(double amount)throws IllegalArgumentException {
        if(amount <= balance)
           balance-=amount;
        else
           // ERROR will occur --> throw exception
           // Method will stop at this point if exception thrown
           throw new IllegalArgumentException("Amount exceeds balance…");
    }

} 
cz289548699 回答:序列化对象数组

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3108456.html

大家都在问