ArrayList可用Java进行序列化

时间:2015-11-05 14:09:16

标签: java serialization arraylist

我遇到了问题,我想将带有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();
                }

            }

3 个答案:

答案 0 :(得分:0)

问题出在这个方法上:

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

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

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

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

答案 1 :(得分:0)

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

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());
    }
    // *************************************************************
}
 }

答案 2 :(得分:-1)

这是因为您的arralylist正在重新初始化 将它改为私人静态决赛,这将使你的一天

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