Arraylist, which is unesterified, is always empty

I had many classes in Java, but this is the first time I have tried to serialize anything. I made my own class, which includes an arraist. The main object is the arrailist of these classes. I believe that I did everything right, but the arraylist is always empty when I read it back.

Main (mainly 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); } } 

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 really appreciate it if someone could point out what I'm doing wrong.

+7
source share
3 answers

This problem has nothing to do with serialization.

You change the reference of passed parameter list inside the load() method, this will not work, since the changed link will only have scope before this method. Change the load 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 the returned object.

  ArrayList<Info> loaded = load(); 
+5
source

There is nothing wrong with the code.

There is a logical problem, though here:

 ArrayList<Info> loaded = new ArrayList <Info>(); load(loaded); 

you are trying to load an empty ArrayList (which is why it doesn't print anything). I assume you want to download, this is a master

 load(master); 
0
source

Try this code ....

result:

[[a], [a], [a]] Loaded list: Loaded size: 3 [[a], [a], [a]]

 package com.palit.java.serialize; import java.io.*; import java.util.ArrayList; import java.util.List; public class Init { 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); List<Info> loaded = new ArrayList<Info>(); loaded = load(); 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 List load() { List list = new ArrayList(); File f = new File("savefile.dat"); try { FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis); try { list = (ArrayList<Info>) ois.readObject(); } catch (ClassNotFoundException cnfe) { System.out.println("Failed to load"); } fis.close(); } catch (IOException ioe) { System.out.println("Failed to load"); } return list; } public static void print(List a) { System.out.println(a); } } 
0
source

All Articles