【问题标题】:ArrayList Serializable with JavaArrayList 可通过 Java 序列化
【发布时间】:2015-11-05 14:09:15
【问题描述】:

我有一个问题,我想用 Java 将 ArrayList 序列化到一个文件中。 然后我想将其反序列化为新的ArrayList 并继续添加到ArrayList

当我反序列化时,它不会加载到ArrayList,它只是打印文件内容。这是我的代码: 这是arraylist类

public class Customers implements Serializable{
ArrayList<Customer> customers = new ArrayList();
ArrayList<Customer> customers2 = new ArrayList();

public void add(Customer customerIn) {
    customers.add(customerIn);
}

public void remove(Customer customerIn) {
    customers.remove(customerIn);
}

public Customer findByName(String firstName, String address) {
    //För varje Customer i customers
    for (Customer customer : customers) {
        if (firstName.equals(customer.getName())) {
            if (address.equals(customer.getAddress())) {
                return customer;
            }
        }
    }
    return null;
}

class for seriallize and deserialize

    public class file {

        public void saveObjectsToFile(Customers customers) {
            try{
                FileOutputStream fos= new FileOutputStream("a.listFile");
                ObjectOutputStream oos= new ObjectOutputStream(fos);
                oos.writeObject(customers);
                oos.close();
                fos.close();
            }catch(IOException ioe){
                ioe.printStackTrace();
            }
        }
        public void takeOutObjectFromFile(Customers customers) {


            try
            {
                FileInputStream fis = new FileInputStream("a.listFile");
                ObjectInputStream ois = new ObjectInputStream(fis);
                customers = (Customers) ois.readObject();
                ois.close();
                fis.close();
                //

                System.out.println(customers);
            }catch(IOException ioe){
                ioe.printStackTrace();
                return;
            }catch(ClassNotFoundException c){
                System.out.println("Class not found");
                c.printStackTrace();
                return;
            }

        }


        class for customer


            //klass customer startar här.
            public class Customer implements Serializable{

                //Variabler int och String för kund id, namn, adress och telefon.
                int CustomerID;
                String customerName, customerAddress, customerPhone, Order;

                //Konstruktor för klassen
                public Customer(String Name, String Address, String Phone, String Order) {
                    this.customerName = Name;
                    this.customerAddress = Address;
                    this.customerPhone = Phone;
                    this.CustomerID = 100001;
                    this.Order = Order;
                }



                //Hämtar och sätter personuppgifter.
                public String getName()     { return this.customerName;     }
                public String getAddress()  { return this.customerAddress;  }
                public String getPhone()    { return this.customerPhone;    }
                public int    getID()       { return this.CustomerID;       }
                public String getOrder()    { return this.Order;            }

                //Skriver ut kontroll av personuppgifter.
                public void printPerson() {
                    System.out.println("\n\nKONTROLL AV UPPGIFTER\n");
                    System.out.println("Namn:\t\t\t" + getName());
                    System.out.println("Adress:\t\t\t" + getAddress());
                    System.out.println("Telefonnummer:\t\t" + getPhone());
                    System.out.println("KundID:\t\t\t" + getID());
                    System.out.println("Order:\t\t\t" + getOrder());

                }
                public String toString() {
                    return getName() + " " + getAddress() + " " + getPhone();
                }

            }

【问题讨论】:

标签: java serialization arraylist


【解决方案1】:

问题出在这个方法上:

    public void takeOutObjectFromFile(Customers customers) {
        ...
            customers = (Customers) ois.readObject();
        ...
    }

您只是覆盖了一个局部变量。你应该拥有的是:

    public Customers takeOutObjectFromFile() {
        ...
            return (Customers) ois.readObject();
        ...
    }

(也可以使用 try-with-resource 来确保您在所有情况下都关闭了文件。)

【讨论】:

    【解决方案2】:

    我解决了我的问题! 现在我可以编辑我的客户了!感谢所有的帮助和建议。

    public class file {
    
    // Get all persons in file
    public  List<Customers> getAllPersons(String fileLocation) {
            List<Customers> localPersons = new ArrayList<>();
    
        try {
            File f = new File(fileLocation);
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            try {
                while (true) {
                    localPersons.add((Customers) ois.readObject());
                }
            } catch (EOFException e) {
    
            }
        } catch (IOException iOException) {
        } catch (ClassNotFoundException classNotFoundException) {
        }
    
            return localPersons;
    }
    
    // Get person on personId in file
    public Customers getPersonOnPersonId(String fileLocation, int personId) {
        Customers localPerson = null;
    
        try {
            File f = new File(fileLocation);
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            try {
                while (true) {
                    Customers tempPerson = (Customers) ois.readObject();
    
                    if(personId == tempPerson.getPersonId()) {
                        localPerson = tempPerson;
                        break;
                    }
                }
            } catch (EOFException e) {
    
            }
        } catch (IOException iOException) {
        } catch (ClassNotFoundException classNotFoundException) {
        }
    
        return localPerson;
    }
    
    // Get persons on firstname in file
    public List<Customers> getPersonsOnFirstName(String fileLocation, String firstName) {
        List<Customers> localPersons = new ArrayList<>();
    
        try {
            File f = new File(fileLocation);
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            try {
                while (true) {
                    Customers tempPerson = (Customers) ois.readObject();
    
                    if(firstName.equals(tempPerson.getFirstName())) {
                        localPersons.add(tempPerson);
                    }
                }
            } catch (EOFException e) {
    
            }
        } catch (IOException iOException) {
        } catch (ClassNotFoundException classNotFoundException) {
        }
    
        return localPersons;
    }
    
    // Get persons on lastname in file
    public List<Customers> getPersonsOnLastName(String fileLocation, String lastName) {
        List<Customers> localPersons = new ArrayList<>();
    
        try {
            File f = new File(fileLocation);
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            try {
                while (true) {
                    Customers tempPerson = (Customers) ois.readObject();
    
                    if(lastName.equals(tempPerson.getLastName())) {
                        localPersons.add(tempPerson);
                    }
                }
            } catch (EOFException e) {
    
            }
        } catch (IOException iOException) {
        } catch (ClassNotFoundException classNotFoundException) {
        }
    
        return localPersons;
    }
    
    // Insert person in file
    public void insertPerson(String fileLocation, Customers person) {
        List<Customers> localPersons = new ArrayList<>();
    
        // Select block ************************************************
        int maxPersonId = 0;
    
        try {
            File f = new File(fileLocation);
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            try {
                while (true) {
                    Customers tempPerson = (Customers) ois.readObject();
                    localPersons.add(tempPerson);
                    if(maxPersonId < tempPerson.getPersonId()) {
                        maxPersonId = tempPerson.getPersonId();
                    }
                }
            } catch (EOFException e) {
    
            }
        } catch (IOException iOException) {
        } catch (ClassNotFoundException classNotFoundException) {
        }
        // *************************************************************
    
        // Set primary key value to the person block *******************
        if(localPersons.isEmpty()) {
            person.setPersonId(1);
        } else {
            maxPersonId++;
            person.setPersonId(maxPersonId);
        }
        // *************************************************************
    
        // Insert block ************************************************
        try {
            File f = new File(fileLocation);
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
    
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            localPersons.add(person);
    
            for(Customers p : localPersons) {
                oos.writeObject(p);
            }
        } catch (FileNotFoundException fileNotFoundException) {
            System.out.println(fileNotFoundException.getMessage());
        } catch (IOException ioexception) {
            System.out.println(ioexception.getMessage());
        }
        // *************************************************************
    }
    
    // Update person in file
    public void updatePerson(String fileLocation, Customers person) {
        List<Customers> localPersons = new ArrayList<>();
    
        // Select block ************************************************
        try {
            File f = new File(fileLocation);
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            try {
                while (true) {
                    Customers tempPerson = (Customers) ois.readObject();
                    if(person.getPersonId() != tempPerson.getPersonId()) {
                        localPersons.add(tempPerson);
                    } else {
                        localPersons.add(person);
                    }
                }
            } catch (EOFException e) {
    
            }
        } catch (IOException iOException) {
        } catch (ClassNotFoundException classNotFoundException) {
        }
        // *************************************************************
    
        // Insert block ************************************************
        try {
            File f = new File(fileLocation);
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
    
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            for(Customers p : localPersons) {
                oos.writeObject(p);
            }
        } catch (FileNotFoundException fileNotFoundException) {
            System.out.println(fileNotFoundException.getMessage());
        } catch (IOException ioexception) {
            System.out.println(ioexception.getMessage());
        }
        // *************************************************************
    }
    
    // Delete person in file
    public void deletePerson(String fileLocation, int personId) {
        List<Customers> localPersons = new ArrayList<>();
    
        // Select block ************************************************
        try {
            File f = new File(fileLocation);
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            try {
                while (true) {
                    Customers tempPerson = (Customers) ois.readObject();
                    if(personId != tempPerson.getPersonId()) {
                        localPersons.add(tempPerson);
                    }
                }
            } catch (EOFException e) {
    
            }
        } catch (IOException iOException) {
        } catch (ClassNotFoundException classNotFoundException) {
        }
        // *************************************************************
    
        // Insert block ************************************************
        try {
            File f = new File(fileLocation);
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
    
            FileInputStream fis = new FileInputStream(f);
            ObjectInputStream ois = new ObjectInputStream(fis);
    
            for(Customers p : localPersons) {
                oos.writeObject(p);
            }
        } catch (FileNotFoundException fileNotFoundException) {
            System.out.println(fileNotFoundException.getMessage());
        } catch (IOException ioexception) {
            System.out.println(ioexception.getMessage());
        }
        // *************************************************************
    }
     }
    

    【讨论】:

      【解决方案3】:

      这是因为您的 arralylist 正在再次初始化 将其更改为私有静态最终版本,这将使您过得愉快

      private static final ArrayList<Customer> customers = new ArrayList();
      

      【讨论】:

      • 什么?不,它应该是Customers 的实例变量。无论如何,可变静态通常是个坏主意。
      猜你喜欢
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2019-03-17
      • 2013-10-22
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多