0

`I am new to java and getting StreamCorruptedException in the code below... In this code I am trying to read multiple objects from a file using ObjectInputStream... m not able to handle the StreamCorruptedException...the o/p I m getting is File C098.txt already exists Product ID:- P001 Description:- Book Price:- Rs.200 Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) at Utility.getProducts(Utility.java:57) at Utility.main(Utility.java:23)

CODE:
import java.io.*;
import java.util.*;
class Product implements Serializable{
    private static final long serialVersionUID = 1L;
    String productId;
    String desc;
    String price;
    public Product(String PId,String a_des,String a_price){
        productId=PId;
        desc=a_des;
        price=a_price;
    }
    public String toString(){
        return "Product ID:- "+productId+"\nDescription:- "+desc+"\nPrice:- "+price;
    }
}
class Utility{
    // Product objProduct;
    public static void main(String args[]) throws Exception{
        String cartId = "C098.txt";
        Product objProduct = new Product("P001","Book","Rs.200");
        addProductToCart(cartId,objProduct);
        getProducts(cartId);
        objProduct = new Product("P087","Laptop","Rs.45,500");
        addProductToCart("C098.txt",objProduct);
        getProducts(cartId);
    }
    public static void addProductToCart(String CId,Product p) throws Exception{
        try{
        boolean searchFile;
        File objFile = new File(CId);
        searchFile = objFile.exists();
        if(searchFile)
            System.out.println("File "+CId+" already exists");
        else{
            objFile.createNewFile();
            System.out.println("File "+CId+" did not exist. It is now created");
        }
        FileOutputStream objFOS = new FileOutputStream(objFile,true);
        ObjectOutputStream objO = new ObjectOutputStream(objFOS);
        objO.writeObject(p);
        objO.flush();
        objO.close();
        }catch(Exception e)
        {
            System.out.println("Exception Caught");
        }
    }
    public static void getProducts(String CId) throws Exception{

        Product objProduct1 = new Product("","","");
        File objFile1 = new File(CId);
        FileInputStream objFIS = new FileInputStream(objFile1);
        ObjectInputStream objI = new ObjectInputStream(objFIS);
        Object obj = null;
        try{
            while((obj=objI.readObject()) != null){
                if (obj instanceof Product) {
                    System.out.println(((Product)obj).toString());
                }
            }
        }catch (EOFException ex) { //This exception will be caught when EOF is reached
            System.out.println("End of file reached.");
        }finally {
            //Close the ObjectInputStream
            try{
                if (objI != null)
                    objI.close();
            }catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}`  
Lizzie
  • 343
  • 2
  • 8
  • 20

2 Answers2

0

The problem is because of header issue, You are appending to same file and while returning second object it throws exception because of headers issue. try to write object in different files, you can rid out of the problem.

SCE Thrown when control information that was read from an object stream violates internal consistency checks. try

    import java.io.*;
    import java.util.*;
    class Product implements Serializable{
        private static final long serialVersionUID = 1L;
        String productId;
        String desc;
        String price;
        public Product(String PId,String a_des,String a_price){
            productId=PId;
            desc=a_des;
            price=a_price;
        }
        public String toString(){
            return "Product ID:- "+productId+"\nDescription:- "+desc+"\nPrice:- "+price;
        }
     // Product objProduct;
        public static void main(String args[]) throws Exception{
            String cartId = "C0982.txt";
            Product objProduct = new Product("P001","Book","Rs.200");
            addProductToCart(cartId,objProduct);
            getProducts(cartId);
            Product  objProduct1 = new Product("P087","Laptop","Rs.45,500");
            addProductToCart("C0981.txt",objProduct1);
            getProducts("C0981.txt");
        }
        public static void addProductToCart(String CId,Product p) throws Exception{
            try{
            boolean searchFile;
            File objFile = new File(CId);
            searchFile = objFile.exists();
            if(searchFile)
                System.out.println("File "+CId+" already exists");
            else{
                objFile.createNewFile();
                System.out.println("File "+CId+" did not exist. It is now created");
            }
            FileOutputStream objFOS = new FileOutputStream(objFile,true);
            ObjectOutputStream objO = new ObjectOutputStream(objFOS);

        objO.writeObject(p);
        objO.flush();
        objO.close();
        }catch(Exception e)
        {
            System.out.println("Exception Caught");
        }
    }
    public static void getProducts(String CId) throws Exception{

        Product objProduct1 = new Product("","","");
        File objFile1 = new File(CId);
        FileInputStream objFIS = new FileInputStream(objFile1);
        ObjectInputStream objI = new ObjectInputStream(objFIS);
        Object obj = null;
        try{
            while((obj=objI.readObject()) != null){
                if (obj instanceof Product) {
                    System.out.println(((Product)obj).toString());
                }
            }
        }catch (EOFException ex) { //This exception will be caught when EOF is reached
            System.out.println("End of file reached.");
        }finally {
            //Close the ObjectInputStream
            try{
                if (objI != null)
                    objI.close();
            }catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
</pre>
user1983527
  • 304
  • 1
  • 7
  • All that `exists/createNewFile()` stuff is a complete waste of time and space. `new FileOutputStream()` will create a new file anyway if necessary. You're forcing the system to test twice and create twice. – user207421 Aug 25 '17 at 00:26
0

You can't 'handle' it. You have to prevent it. It results from a design error such as using two ObjectOutputStreams on a stream that is read by a single ObjectInputStream, as you are doing here by appending to the file, or writing data other than objects and not reading it symmetrically.

user207421
  • 305,947
  • 44
  • 307
  • 483