I need help with reading an array list from a Serialized text file, I do not believe I am having a problem with it however I cannot see if it is actually storing all of the information because I cant print it out to test it, here are the code snipets:
and when I do read from the file I get the ArrayList cannot be cast to StoreAddress
FileInputStream ios = new FileInputStream("C:\\Users\\Mr Cata\\Desktop\\Testingoutput.txt");
ObjectInputStream ois = new ObjectInputStream(ios);
StoreAddress SAR = (StoreAddress)ois.readObject();
ArrayList<StoreAddress> ALStore = (ArrayList)ois.readObject();
for(int i = 0; i < ALStore.size(); i++){
String list = ALStore.get(i).toString();
}
ios.close();
next
for(int i = 0; i < ALStore.size(); i++){
ALStore.get(i);
}
…
for(int i = 0; i < ALStore.size(); i++){
if (i >= ALStore.size())
{
ALStore.add(SA);}
}
FileOutputStream fos = new FileOutputStream("C:\\Users\\Mr Cata\\Desktop\\Testingoutput.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(ALStore);
fos.close();
}
When you are writing into the file , you write like
Here you are writing the
which is the array list you are mentioning. But when you are reading, your snippet looks like
what you have to notice here is you have have written only array list object, but not StoreAddress. If so, what is the need for the line
in the snippet ? This is the problem here. Remove this and it should work fine.