2

I'm learning now how to do serialization using Java Language. I have read some posts and docs about the subject and I tried to do a simple example (below)

public class SterializeObject implements java.io.Serializable{
 /**
 * 
 */
 private static final long serialVersionUID = 1L;
 private String name;
 private transient int code;
 public SterializeObject (String n, int c){
 name = n;
 code = c;
 }
 public void printAtributes (){
 System.out.println("name: " + name + "; code: " + code);
 }
 }
 public class MainClass {
 public static void main(String[] agrs) {
 SterializeObject ob1 = new SterializeObject("ana", 1);
 SterializeObject ob2 = new SterializeObject("rita", 2);
 try {
 FileOutputStream fileOut = new FileOutputStream("file.data");
 ObjectOutputStream outObj = new ObjectOutputStream(fileOut);
 outObj.writeObject(ob1);
 outObj.writeObject(ob2);
 outObj.close();
 System.out.println("Objects were serialized!");
 } catch (IOException e) {
 e.printStackTrace();
 }
 ArrayList<SterializeObject> list = new ArrayList<SterializeObject>();
 try {
 FileInputStream fileInput = new FileInputStream("file.data");
 ObjectInputStream inputObj = new ObjectInputStream(fileInput);
 Object o;
 try {
 while ((o = inputObj.readObject()) != null) {
 list.add((SterializeObject) o);
 }
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 System.out.println("Erro foi aqui! (1)");
 }
 inputObj.close();
 fileInput.close();
 } catch (IOException e) {
 e.printStackTrace();
 System.out.println("Erro foi aqui! (2)");
 }
 for (int i = 0; i < list.size(); ++i) {
 list.get(i).printAtributes();
 }
 }
 }

I created a Class SterializeObject that implements java.io.Serializable with two variables: one string (name) and one int (code) that is transient. Then In the main I generate two instances of that class and I tried to write it in a file, that I have done successfully! After that, I try to read the two object with a Loop.. there is my problem.. since the ObjectInputStream dosen't have some kind of method to see if we are in the end or not. So, I tried to do with this condition: (o = inputObj.readObject()) != null.

My output is this:
java.io.EOFException
 at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
 at java.io.ObjectInputStream.readObject0(Unknown Source)
 at java.io.ObjectInputStream.readObject(Unknown Source)
 at MainClass.main(MainClass.java:30)
Objects were serialized!
Erro foi aqui! (2)
name: ana; code: 0
name: rita; code: 0

I get the objects, but I get an error because, I think, is trying to access to something that doesn't exist.

Someone can tell me other way to do it?

Best Regards.

asked Apr 3, 2013 at 13:05
2
  • readObject throws an exception when it reaches the end of the file, it does not return null as your code seems to imply. Commented Apr 3, 2013 at 13:11
  • I dind't know how to do in other way , so I tried like that! Commented Apr 3, 2013 at 13:13

6 Answers 6

4

Read as many objects as the number of written objects, or write the list of objects itself, instead of writing every object one after the other.

(Or rely on the EOFException to detect the end of the stream, but this is ugly).

answered Apr 3, 2013 at 13:09
Sign up to request clarification or add additional context in comments.

2 Comments

Hum.. I'm sorry for the question, but How can write the list of objects itself"? thanks
You store both objects in an ArrayList<SterializeObject>, and call writeObject(arrayList). On the receiving side, you read one object and cast it to List<SterializeObject>.
2

As many of you told me to do, I created a ArrayList and serialized the ArrayList. My code is:

public class MainClass {
 public static void main(String[] agrs) {
 SterializeObject ob1 = new SterializeObject("ana", 1);
 SterializeObject ob2 = new SterializeObject("rita", 2);
 ArrayList <SterializeObject> list = new ArrayList<>();
 list.add(ob1);
 list.add(ob2);
 ArrayList <SterializeObject> input = new ArrayList<SterializeObject>();
 try {
 FileOutputStream fileOut = new FileOutputStream("file.data");
 ObjectOutputStream outObj = new ObjectOutputStream(fileOut);
 outObj.writeObject(list);
 outObj.close();
 System.out.println("Objects were serialized!");
 } catch (IOException e) {
 e.printStackTrace();
 }
 try {
 FileInputStream fileInput = new FileInputStream("file.data");
 ObjectInputStream inputObj = new ObjectInputStream(fileInput);
 Object o;
 try {
 input = (ArrayList<SterializeObject>) inputObj.readObject();
 } catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 System.out.println("Erro foi aqui! (1)");
 }
 inputObj.close();
 fileInput.close();
 } catch (IOException e) {
 e.printStackTrace();
 System.out.println("Erro foi aqui! (2)");
 }
 for (int i = 0; i < input.size(); ++i) {
 input.get(i).printAtributes();
 }
 }
}

And the output is: Objects were serialized! name: ana; code: 0 name: rita; code: 0

Thank you for the help!

answered Apr 5, 2013 at 14:55

Comments

0

Close the FileOutputStream also along with ObjectOutputStream

fileOut.close();
answered Apr 3, 2013 at 13:11

1 Comment

That doesn't answer the question, and it's wrong. Closing the ObjectOutputStream automatically closes the wrapped FileOutputStream.
0

Why don't you add both object to an ArrayList, and serialize the ArrayList. Then you just have to Deserialize the ArrayList and it will be populated with both objects.

answered Apr 3, 2013 at 13:12

Comments

0

You can do this by placing the readObject call inside a try-catch block and catching that EOFException you get, signaling you have read all the objects.

answered Apr 3, 2013 at 13:13

Comments

0

Replace your while loop with this piece of code

do{
 try
 {
 o = inputObj.readObject();
 list.add((SterializeObject) o);
 }
 catch(EOFException e)
 {
 o = null;
 }
}while (o != null);
answered Apr 3, 2013 at 13:21

1 Comment

Thank you for your help! I have created an ArrayList but this way is good too! I will try too! Thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.