• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

What is serialized???

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi frnds,
can anybody tell me about serialized in java
thanx
aftab
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Serialization is the process inloved to make your objects Persistence. Persistence means something which keeps on living does not die with its initial or intermediate enviornment.
Simply put you can create a Object and Persist it on your file system, in your computer memory, some network storage or network it self.
To enable a object to serialize your object's class needs to either implement Serializable(if you want to go along with the given serialization mechanism) or implement Externalizable(If you want to define customized way to persist an object).
Both interfaces are defined in java.io and ObjectInputStream and OutputStream can be used to read/write your objects to streams, now it must be understood that the end streams could be a Socket streams, File stream or memory streams.
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For you to practice an example can be written out as follows;
import java.io.*;
public class Persistence{
public static void main(String[] args){
System.out.println("Attempting to write an Object to a file....");
try{
FileOutputStream fos = new FileOutputStream ("AnyFile.bcd");
MyObject mob = new MyObject("I defined this Object");
ObjectOutputStream o = new ObjectOutputStream(fos);
o.writeObject(mob);
o.close();
fos.close();
} catch(IOException ioe){}
System.out.println("Now attempting to read an saved Object from a file");
try{
FileInputStream fis = new FileInputStream("AnyFile.bcd");
ObjectInputStream o = new ObjectInputStream(fis);
System.out.println(fis.readObject());
o.close();
fis.close();
}catch(IOException ioe){}
}
}
class MyObject implements Serializable{
private String aString;
MyObject(String anyString){
aString = anyString;
}
public String toString(){
return aString;
}
}

You should copy and try to compile the code to gain some experience.
And also once that output file is created, do try to view that bcd file as well.


[This message has been edited by Amond Adams (edited December 01, 2000).]
[This message has been edited by Amond Adams (edited December 01, 2000).]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amond, I am getting compile error, Could you please explain the program in detail??
C:\jdk1.3\bin>javac Persistence.java
Persistence.java:26: cannot resolve symbol
symbol : method readObject ()
location: class java.io.FileInputStream
System.out.println(fis.readObject());
Thanx
Vidya
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vidya,
It shoud be o.readobject instead of fin.readobject and also you need to catch or throw classnotfound exception. Modified code will be,
import java.io.*;
public class Persistence{
public static void main(String[] args){
System.out.println("Attempting to write an Object to a file....");
try{
FileOutputStream fos = new FileOutputStream ("AnyFile.bcd");
MyObject mob = new MyObject("I defined this Object");
ObjectOutputStream o = new ObjectOutputStream(fos);
o.writeObject(mob);
o.close();
fos.close();
} catch(IOException ioe){}
System.out.println("Now attempting to read an saved Object from a file");
try{
FileInputStream fis = new FileInputStream("AnyFile.bcd");
ObjectInputStream o = new ObjectInputStream(fis);
System.out.println(o.readObject());
o.close();
fis.close();
}catch(IOException ioe){}
catch (ClassNotFoundException e) {}
}
}
Saumil
 
Vidya Selvaraj
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Saumil & Amond, now I am clear
Regards
Vidya
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, tried the code
When I remove 'implements Serializable' from MyObject class
There's neither compiler error nor runtime but when i check
the file "AnyFile.bcd" there is
�� {sr java.io.NotSerializableException(Vx ��5 xr java.io.ObjectStreamExceptiond��k�9�� xr java.io.IOExceptionl�sde%�� xr java.lang.Exception��>;� xr java.lang.Throwable��5'9w�� L detailMessaget Ljava/lang/String;xpt MyObject
I think all exceptions are thrown during runtime but in this
case exception is send through ouputstream to the file
please explain.
 
saumil shukla
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nasir,
Serializable is just a tag, but in basence of it,I/O's won't let you serialize and hence throwing exception, current code is catching the IOException but not doing anythign about it try printing stacktrace or something like that.
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Vidya for that typographical error, my java is not used to hurriness. And I see one good buddy of ours rightly corrected it.
And for nasir's last post here, if you remove implements Serializable declaration from from user defined classes, as is in the case presented by nasir, the code will throw a NotSerializableException not an explicit IOException. Though as NotSerializableException indirectly extends IOException you can catch it with the existent code and to debug just add ioe.printStackTrace(System.err) in between catch block.

Hope this makes

[This message has been edited by Amond Adams (edited December 02, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic