I've had a lot of classes in Java, but this is the first time I've tried to serialize anything. I've made my own class which includes an arraylist. The main object is an arraylist of these classes. I believe I have done everything right but the arraylist is always empty when I read it back in.
The main (mostly a test) class:
import java.io.*;
import java.util.ArrayList;
public class IOTest {
public static void main(String[] args) {
ArrayList<Info> master = new ArrayList <Info>();
Info a = new Info("a");
Info b = new Info("a");
Info c = new Info("a");
master.add(a);
master.add(b);
master.add(c);
print(master);
save(master);
ArrayList<Info> loaded = new ArrayList <Info>();
load(loaded);
System.out.println("Loaded List:");
System.out.println("Loaded Size:" + loaded.size());
print(loaded);
}
public static void save(ArrayList a){
File f = new File("savefile.dat");
try {
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(a);
fos.flush();
fos.close();
} catch (IOException ioe) {
System.out.println("Failed to save");
};
}
public static void load(ArrayList a){
File f = new File("savefile.dat");
try {
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
try {
a = (ArrayList<Info>) ois.readObject();
} catch (ClassNotFoundException cnfe) {
System.out.println("Failed to load");
}
fis.close();
} catch (IOException ioe) {
System.out.println("Failed to load");
}
}
public static void print(ArrayList a){
System.out.println(a);
}
}
The custom data structure:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class Info implements Serializable {
private static final long serialVersionUID = -5781559849007353596L;
ArrayList<String> list;
public Info( String e) {
list = new ArrayList<String>();
list.add(e);
}
@Override
public String toString() {
return list.toString();
}
private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException {
aInputStream.defaultReadObject();
}
private void writeObject(ObjectOutputStream aOutputStream) throws IOException {
aOutputStream.defaultWriteObject();
}
}
I would be very grateful if someone can point out what I'm doing wrong.
This problem has nothing to do with serialization.
You are changing reference of passed parameter
list inside load()
method this will not work since changed reference will have only scope until that method. Change load to below
public static ArrayList<Info> load() {//Change return type
File f = new File("savefile.dat");
try {
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
try {
ArrayList<Info> a = (ArrayList<Info>) ois.readObject();//get the object that is read
return a;
} catch (ClassNotFoundException cnfe) {
System.out.println("Failed to load");
}
fis.close();
} catch (IOException ioe) {
System.out.println("Failed to load");
}
return null;
}
And use returned object.
ArrayList<Info> loaded = load();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With