• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Serialization question

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm studying for the SCJP exam using the great Sierra/Bates book...
And... Well, I tried to replicate some serialization code examples and it does not work as expected... here's the code:

import java.io.*;

class NsClass {
int i;

public void setI(int i) {
this.i = i;
}

public int getI(){
return this.i;
}
}

public class SerialTest extends NsClass implements Serializable{
String myVar;

public void writeObject (ObjectOutputStream oos) throws IOException, ClassNotFoundException {
oos.defaultWriteObject();
oos.writeInt(super.getI());
}

public void readObject (ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
super.setI(ois.readInt());
}

public static void main(String [] args) {

SerialTest s1 = new SerialTest();
SerialTest s2;
s1.myVar = "this is my var...";
s1.setI(15);

//write object
try {
FileOutputStream fos = new FileOutputStream("fos.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s1);
oos.close();


//Read written object
FileInputStream fis = new FileInputStream("fos.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
s2 = (SerialTest)ois.readObject();
System.out.println("Reading s2 var..." + s2.myVar);
System.out.println("Reading i var..." + s2.i);
}
catch (Exception ex) {
System.out.println("Exception " + ex.toString());
}
}

}

The thing is that in this custom serialization the variable i is not getting saved into the file fos.txt, so when I deserialize it and print it the result is:

Reading s2 var...this is my var...
Reading i var...0

What am I doing wrong?
Please help...
Thanks in advance
Marx
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As it is your writeObject and readObject methods aren't being called because they have the wrong method signitures. Change the access specifiers on your writeObject & readObject methods to private and it will work.
 
Marx Villegas
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That explains everything...
Thanks a lot
Marx
 
A timing clock, fuse wire, high explosives and a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic