-
Java Serialization - Problem..
Hi..
I want to write the "Person" object to the "phone.dat " file but i have a problem while reading..
class Person
{
int i;
public Person(int i)
{
this.i = i;
}
}
File file = new File("phone.dat");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,true)); //true = append mode
Person myClass = new Person( 5 ); //my number 5
oos.writeObject(myClass);
oos.close
I write this code and it performed successfully. Another time , i want to append another "Person" object. Also this was successfull (The file size increased succesfully)
File file = new File("phone.dat");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,true)); //true = append mode
Person myClass = new Person( 17 ); //my number 17
oos.writeObject(myClass);
oos.close
But when i want to read the objects from the file:
File file = new File("phone.dat");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
Person class1= (Person )ois.readObject();
Person class2= (Person )ois.readObject();
System.out.println ( class1.i ); //prints "5"
System.out.println( class2.i ); //nothing prints...
ois.close;
Could you explain why i can't read the second object? I have a project so this is very important for me..
Thank you very much.
Similar Threads
-
Replies: 3
Last Post: 03-21-2007, 03:28 PM
-
Replies: 0
Last Post: 04-27-2006, 05:29 PM
-
Replies: 2
Last Post: 04-30-2003, 05:52 PM
-
By Allan Huang in forum Java
Replies: 0
Last Post: 02-20-2002, 12:05 AM
-
By javachild in forum Java
Replies: 0
Last Post: 08-10-2000, 01:52 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks