Results 1 to 4 of 4
- 02-28-2011, 07:19 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Exception in object deserialization
i get this exception while reading another object from file:java.io.StreamCorruptedException.. well this is my program and its output.. can anyone help me.. and also in the record file , it has multiple object of different data...
And this is the output i get when i choose option 2 to view records. It only shows the first record. When it goes to second record this exception occurs:Java Code:import java.util.*; import java.io.*; class SchoolDBS2 { public static ObjectOutputStream out = null; public static ObjectInputStream in = null; public static Scanner kb = null; public static SchoolData data; public static final String filename = "Records.dat"; public static void main(String args[]) { int choice; kb = new Scanner(System.in); do { System.out.println(" School DATABASE"); System.out.println(""); System.out.println("Enter 1: To enter record"); System.out.println("Enter 2: To view record"); System.out.println("Enter 3: To search number of record"); System.out.println("Enter 9: To exit"); System.out.println(""); System.out.println("Enter Your Choice: "); choice = kb.nextInt(); if(choice == 1) { try { String t_name,t_gender; int t_age; out = new ObjectOutputStream(new FileOutputStream(filename,true));; System.out.println(""); System.out.println("Enter the data for approriate fields"); System.out.println("Name : "); t_name = kb.next(); System.out.println("Gender : "); t_gender = kb.next(); System.out.println("Age : "); t_age = kb.nextInt(); data = new SchoolData(t_name,t_age,t_gender); out.writeObject(data); out.reset(); } catch(Exception e) { System.out.println("Error 101"); System.out.println(e); } finally { try{out.close(); System.out.println("File is closed in OutputStream");} catch(Exception e){System.out.println(e);} } } else if(choice == 2) { try { in = new ObjectInputStream(new FileInputStream(filename)); SchoolData temp; System.out.println("No of bytes available is " + in.available()); while(true) { temp = (SchoolData)in.readObject(); temp.show(); } } catch(EOFException i) { } catch(Exception e) { System.out.println("Error 102"); System.out.println(e); e.printStackTrace(); } finally { try{out.close(); System.out.println("File is closed in InputStream");} catch(Exception e){System.out.println(e);} } } else if(choice == 3) { } }while(choice!=9); }// end of main }// end of class //Class School Data for object serialization in another file.. import java.io.*; class SchoolData implements Serializable { //public static final String R_Strt = "RCR_STRT"; public String name; public int age; public String gender; //public static final String R_End = "RCR_END"; SchoolData(String nme, int ag, String gen) { this.name = nme; this.age = ag; this.gender = gen; } void show() { System.out.println("Name : " + name); System.out.println("Age : " + age); System.out.println("Gender : " + gender); } }
Enter Your Choice:
2
No of bytes available is 0
Name : Tim
Age : 13
Gender : F
Error 102
java.io.StreamCorruptedException: invalid type code: AC
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at SchoolDBS2.main(SchoolDBS2.java:78)
- 02-28-2011, 07:46 PM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
You are calling 'reset' on the ObjectOutputStream, which causes it to disregard whatever has previously been written. I think you meant to call 'flush' rather than reset.
- 02-28-2011, 07:49 PM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
Here's an excellent explanation from another Java forum:
java - StreamCorruptedException: invalid type code: AC - Stack Overflow
- 03-01-2011, 07:13 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Singleton serialization / deserialization
By DerekRaimann in forum New To JavaReplies: 4Last Post: 02-28-2011, 01:38 AM -
[SOLVED] Deserialization question
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 04-14-2009, 12:15 AM -
Null Pointer Exception for formatter object
By andre1011 in forum NetworkingReplies: 0Last Post: 03-28-2009, 10:51 PM -
Serialization/Deserialization Error
By andrepezzo in forum Advanced JavaReplies: 2Last Post: 12-16-2008, 05:36 PM -
Serialization/Deserialization Error
By andrepezzo in forum NetworkingReplies: 0Last Post: 12-16-2008, 04:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks