2015-11-05 25 views
-1

我遇到了一個問題,我想用Java將一個ArrayList序列化爲一個文件。 然後我想將其反序列化到一個新的ArrayList並繼續添加到ArrayList用Java可序列化的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(); 
       } 

      } 
+0

的可能的複製[系列化的ArrayList的Java(http://stackoverflow.com/questions/8785955/serialization-arraylist-java) – Tgsmith61591

+0

你的意思是 –

回答

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

這是因爲你的arralylist再次被初始化 將其更改爲私有靜態最後這將使你的一天

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

大會嘗試這對希望工程 –

+0

什麼?不。它應該是「客戶」的實例變量。無論如何,一般來說可變靜力學是一個壞主意。 –

0

問題是與這種方法:

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

你已經簡單地覆蓋了一個局部變量。你應該擁有的是:

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

(同時使用try-與資源以確保您在所有的情況下關閉了文件)