Thursday, October 11, 2007

Java Faqs - How do I serialize an object to a file ?

Answer : To serialize an object into a stream perform the following actions:

- Open one of the output streams, for exxample FileOutputStream

- Chain it with the ObjectOutputStream <- Call the method writeObject() providinng the instance of a Serializable object as an argument.

- Close the streams

Java Code ---------

try{

fOut= new FileOutputStream("c:\\emp.ser");

out = new ObjectOutputStream(fOut);

out.writeObject(employee); //serializing

System.out.println("An employee is serialized into c:\\emp.ser");

} catch(IOException e){ e.printStackTrace();

}

1 comment:

Chitta Ranjan Dash said...

Know more about Java Coding by browsing the below site's code
Herbspro and be healthy. bcoz health is wealth