I am trying to save and load a single Java object with serialization. I managed to make it save it to a file and from the looks of it it contains the data correctly (I can "read" the file with a text editor and somewhat distinguish the contents of arrays and variables in there)
protected static void saveChat(Chats chat1) throws IOException {
try(FileOutputStream f = new FileOutputStream("data.txt");
ObjectOutput s = new ObjectOutputStream(f)) {
s.writeObject(chat1);
}
}
protected static void loadChat(Chats chat1) throws FileNotFoundException, IOException,
ClassNotFoundException {
try(FileInputStream in = new FileInputStream("data.txt");
ObjectInputStream s = new ObjectInputStream(in)) {
chat1 = (Chats) s.readObject();
}
}
The problem comes when I try to load the (apparently) saved object chat1
with loadChat
, which doesn't seem to do anything and doesn't load/write any data. I am not sure if I am missing anything.