ObjectInputStream returns a wrong boolean value of a object
ObjectOutputStream.writeObject once, then change the boolean value of the Contact object, ObjectOutputStream.writeObject again, but when ObjectInputStream.readObject, it returns SAME boolean values which should be different. can somebody explain me this? please. thx
1.import java.io.*;
2.import java.util.*;
3.
4.publicclass TestSerializable{
5.
6./** Creates a new instance of TestSerializable */
7.publicstaticvoid main(String args[]){
8.new TestSerializable();
9.}
10.
11.public TestSerializable(){
12.try{
13. Contact contact =new Contact("email","group",1);// contact.isOnline = true for default;
14.
15. FileOutputStream fos =new FileOutputStream("ser");
16. ObjectOutputStream oos =new ObjectOutputStream(fos);
17.
18. oos.writeObject(contact);
19.
20. contact.setOnline(false);
21. oos.writeObject(contact);
22.
23. oos.flush();
24.
25.
26. FileInputStream fis =new FileInputStream("ser");
27. ObjectInputStream ois =new ObjectInputStream(fis);
28. Contact ccRead1 = (Contact)ois.readObject();
29. Contact ccRead2 = (Contact)ois.readObject();
30.
31. System.out.println("ccRead1.isOnline() = "+ccRead1.isOnline());
[u] 32. System.out.println("ccRead2.isOnline() = "+ccRead2.isOnline() +" which should be FALSE. ERROR"); [/u]
33.
34. oos.close();
35. ois.close();
36.
37.}
38.catch(Exception e){
39. e.printStackTrace();
40.}
41.
42.}
43.
44.}
45.
46.
47.
48.
49.
50.import javax.swing.tree.DefaultMutableTreeNode;
51.import javax.swing.tree.*;
52.import java.util.*;
53.
54.publicclass Contactextends DefaultMutableTreeNode{
55.
56.private String email;
57.privateint arrayIndex;
58.private String group;
59.privateboolean online;
60.
61.
62.public Contact(String email,String group,int arrayIndex,boolean online){
63. this.email = email;
64. this.group = group;
65. this.setArrayIndex(arrayIndex);
66. this.setOnline(online);
67. this.setUserObject(this);
68. this.setAllowsChildren(false);
69.}
70.
71.public Contact(String email, String group){
72.this(email,group,-1,false);
73.}
74.
75.public Contact(String email, String group,int arrayIndex){
76.this(email,group,arrayIndex,true);
77.}
78.
79.
80.
81.public String toString(){
82.return this.email;
83.}
84.
85.public String getEmail(){
86.return email;
87.}
88.
89.publicint getArrayIndex(){
90.return arrayIndex;
91.}
92.
93.public String getGroup(){
94.return group;
95.}
96.
97.publicboolean isOnline(){
98.return online;
99.}
100.
101.publicvoid setOnline(boolean online){
102. this.online = online;
103.}
104.
105.publicvoid setArrayIndex(int arrayIndex){
106. this.arrayIndex = arrayIndex;
107.}
108.
109.
110.}

