Home Discussion Discuss: Deserializing an Object in java

  • SerializationSaeed Ahmad May 31, 2011 at 4:51 PM

    import java.io.*; public class SerializationDemo { public static void main(String args[]) { // Object serialization try { MyClass object1 = new MyClass("Hello", -7, 2.7e10); System.out.println("object1: " + object1); FileOutputStream fos = new FileOutputStream("serial"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object1); oos.flush(); oos.close(); } catch(Exception e) { System.out.println("Exception during serialization: " + e); System.exit(0); } // Object deserialization try { MyClass object2; FileInputStream fis = new FileInputStream("serial"); ObjectInputStream ois = new ObjectInputStream(fis); object2 = (MyClass)ois.readObject(); ois.close(); System.out.println("object2: " + object2); } catch(Exception e) { System.out.println("Exception during deserialization: " + e); System.exit(0); } } } class MyClass implements Serializable { String s; int i; double d; public MyClass(String s, int i, double d) { this.s = s; this.i = i; this.d = d; } public String toString() { return "s=" + s + "; i=" + i + "; d=" + d; } }

  • not clearmuruga November 3, 2011 at 6:01 PM

    it will an more notes

  • File not found errortest January 26, 2012 at 8:09 PM

    Hi I am tryin to run this example but getting the above error. I have tried to put file in the same folder where code is but still not working. Any help please.

Post your Comment



Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Related Articles
Java Interview Questions

Java object
Java object  Which class should you use to obtain design information about an object 

Java Interview Questions

Java object
Java object  What is mutable object and immutable object?  An Immutable object is a kind of object whose state cannot be modified after it is created. This is as opposed to a mutable object, which can be modified 

Java Beginners

object creation
object creation  when will java object is created? is it at runtime or compiletime 

Java Interview Questions

Object Oriented
Object Oriented  C++ is Purely object oriented or not .Then why java called purely object oriented 

General

Object
Object       An object...). In an object, variables store values for later use and methods are the unit... are the basic units of the object-oriented programming. Objects are the part of our day 

Core Java

Java Object
Java Object      ... properties of the class or its group. Java object is an instance of the class. It takes.../java-object-oriented-language.shtml   

Servlet Interview Questions

Session Object
Session Object  Why do we require Session Object?  Hello, In java HttpSession is an interface present in javax.servlet.http package. The session object is used by the developers to store and retrieve user's 

Java Beginners

object as argument
object as argument  what happens when we pass a object into a method???if java is pass by value how does this makes a difference.....pllzzz give me more clarification on why we pass objects in a margument 

Java Beginners

Object in java - Java Beginners
Object in java  Hi deepak, I am new to java, can u tell me what is class ,object and instance with example. For example : class raja , raja r.../java/master-java/java-object-oriented-language.shtml http 

Java Interview Questions

Java object clone
Java object clone  What is criteria for a object to get cloned 

Java Beginners

java object - Java Beginners
java object  i want a complete memory description of objects,methods... of objects. The primitive data type and keyword void is work as a class object. Object: Object is the basic entity of object oriented programming language 

Java Interview Questions

Java object's lock
Java object's lock  What is an object's lock and which object's have locks 

JSP Servlet Questions

implect object
implect object  how i implment implect object   Hi Friend, Please visit the following links: http://www.roseindia.net/help/java/o/jsp-implicit-objects.shtml http://www.roseindia.net/jsp/jspfundamentals.shtml Thanks 

Java Beginners

Vector object[]
Vector object[]  I want to create payslip in java public Vector...(); Object[] value = new Object[] {"Employee Name",f.ename.getSelectedItem()}; rec.add(value); value = new Object[] {"Mary-Kate Olsen", "Ashley 

Java Beginners

java object - Java Beginners
java object  i want to where in the memory the java objects,local... are stayed  Hi friend, Instance Variables (Non-static fields): In object... fields" that is declared without the static keyword. Each object of the class has its 

Java Beginners

java object class methods
java object class methods  What are the methods in Object class?  There are lots of methods in object class. the list of some methods are as- clone equals wait finalize getClass hashCode notify notifyAll 

Hibernate Interview Questions

Map java Object to database.
Map java Object to database.  How do you map Java Objects with Database tables 

Java Beginners

Object class in java
Object class in java  want an example and with an explaination about equals() method in object class? Is equals() is similar 

Java Interview Questions

java is pure object oriented
java is pure object oriented  java is pure object oriented or not.? reason.?   Java is not pure object oriented language because... are not object 2)It does not support operator overloading multiple inheritance. 3 

JSP Servlet Questions

bean object
"); rd.forward(req, res); } } 3)beandata.jsp: <%@page language="java...; For more information, visit the following links: http://www.roseindia.net/jsp/java 

Java Beginners

object creation - Java Beginners
object creation  I need object creation in depth(with stack,pc registers).Any one can provide me if possible with video/audio 

Java Beginners

object conversion - Java Beginners
object conversion   Hi, Can anybody tell me the object conversion in java.   Hi sandeep Can u please tell me in details about your query, otherwise u may got help from here, http://www.roseindia.net/java 

Java Interview Questions

object - Java Interview Questions
?   Hello, i know only two wayes of object instansiations 1.... Thankx   Hi Friend, Yes we can create an object in the following... of creating an object. ClassName classObject = new ClassName(); 2. Using 

Java Beginners

Object serialization - Java Beginners
Object serialization  Design classes for Currency, Rupee, and Dollar. Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization. Write another program to read 

Java Beginners

vector object - Java Beginners
vector object  hi i have created a Vector vec=new Vector(); vec.addElement(o); o is an object of group(acl.group) what i have to do to read contents of this object? can it be possible to convert it into string arrays 

Java Server Faces Questions

java : object oriented
java : object oriented   (a) Write a class named CarRental... the user for the data needed for a rental and creates an object of the correct... an object of the correct type. Display the total rental fee 

Java Beginners

Is Java a pure object oriented language?
Is Java a pure object oriented language?   Hi, Is Java a pure object oriented language? thanks   Hi No, Java is an object oriented programming language but not purely a object oriented language. In OOPs programming 

Java-Tutorials

Deserializing an Object in java
Deserializing an Object in java     .... The given program shows how to read any data or contents from the serialized object or file. It takes a file name and then converts into java object. If any exception 

Java Beginners

object retun type - Java Beginners
object retun type  Hi, i have to create a method which should have return type of object . The object should contain 'list of filenames' array and a boolean field 'isVal'. Can anybody help 

Java Beginners

object 2 string - Java Beginners
object 2 string  hi sir,how to convert object to string and how 2 compare object with null value to string with null value plz provide a program 4 this sir,plzzzzzzzzz 

DMCA.com