2011-04-03 32 views
0

我不知道,我非常理解序列化的概念,我的問題是這樣的:每次一個ArrayList添加到序列化的文件

我上課的ArrayList,我稱之爲歷史:

public class History implements Serializable 
{ 

private static final long serialVersionUID = 1L; 

String nameOfMedicaments,DCI,pathologie,nameOfToCountry; 
String description; 
int DCIN; 


public String getNameOfMedicaments() { 
    return nameOfMedicaments; 
} 
public void setNameOfMedicaments(String nameOfMedicaments) 
{ 
    this.nameOfMedicaments = nameOfMedicaments; 
} 

public int getDCIN() { 
    return DCIN; 
} 
public void setDCIN(int DCIN) 
{ 
    this.DCIN = DCIN; 
} 

public String getDCI() { 
    return DCI; 
} 
public void setDCI(String DCI) 
{ 
    this.DCI = DCI; 
} 
public String getPathologie() { 
    return pathologie; 
} 
public void setPathologie(String pathologie) 
{ 
    this.pathologie = pathologie; 
} 

public String getNameOfToCountry() { 
    return nameOfToCountry; 
} 
public void setNameOfToCountry(String nameOfToCountry) 
{ 
    this.nameOfToCountry = nameOfToCountry; 
} 
public String getDescription() { 
    return description; 
} 
public void setDescription(String description) 
{ 
    this.description = description; 
} 


} 

序列化ArrayList中我使用此功能:

History history=new History(); 
ArrayList<History> results = new ArrayList<History>(); 
    results.add(history); 

      FileOutputStream fos; 
      try { 
       fos = openFileOutput("history", Context.MODE_PRIVATE); 
       ObjectOutputStream oos = new ObjectOutputStream(fos); 
       oos.writeObject(results); 
       oos.flush(); 
       oos.close(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 

現在我想,每當我添加一個ArrayList時間序列化不會擦除舊數據,但添加到鄰一個新陣列或其他更好的,我成功得到最後一個,

但我不知道我應該做什麼來獲得所有的arrayList。

感謝您的幫助!

ps:抱歉我的英語!

回答

相關問題