I have a class to write customer objects into an arraylist and write this to a file.Each write will append to the end of the file. But when I try to read all of the data in the file it only displays the first object stored in my arraylist.
public void readFromFile(){
ArrayList <Customer> list;
try{
final String FILENAME = "..//Files//customer.bin";
FileInputStream fi = new FileInputStream(FILENAME);
ObjectInputStream os = new ObjectInputStream(fi);
list = (ArrayList)os.readObject();
for(Customer cus : list){
System.out.println(cus);
}
os.close();
fi.close();
}
How can I display all the data in the file and not just the first write??
public void writeToFile(){
if(customerList instanceof Serializable){
try{
final String FILENAME = "..//Files//customer.bin";
FileOutputStream fo = new FileOutputStream(FILENAME,true);
ObjectOutputStream os = new ObjectOutputStream(fo);
os.writeObject(customerList);
os.close();
fo.close();
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException io){
System.out.println("Error initaling stream...");
}
catch(Exception w){
System.out.println(w.getMessage());
}
}
}