Results 1 to 4 of 4
  1. #1
    hanx is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default 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...

    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);
    	}
    	
    }
    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:
    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)

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    10

    Default

    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.

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Here's an excellent explanation from another Java forum:

    java - StreamCorruptedException: invalid type code: AC - Stack Overflow

  4. #4
    hanx is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    toadaly, eh it didnt help... @NRitH,, thanks for the link it brought me to some idea.. thanks both of ya...

Similar Threads

  1. Singleton serialization / deserialization
    By DerekRaimann in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 02:38 AM
  2. [SOLVED] Deserialization question
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 04-14-2009, 01:15 AM
  3. Null Pointer Exception for formatter object
    By andre1011 in forum Networking
    Replies: 0
    Last Post: 03-28-2009, 11:51 PM
  4. Serialization/Deserialization Error
    By andrepezzo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2008, 06:36 PM
  5. Serialization/Deserialization Error
    By andrepezzo in forum Networking
    Replies: 0
    Last Post: 12-16-2008, 05:21 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
  •