使用ObjectInputStream时如何处理StreamCorruptedException?

时间:2013-01-18 12:11:31

标签: java

`我是java新手并在下面的代码中获取StreamCorruptedException ...在这段代码中我试图使用ObjectInputStream从文件中读取多个对象... m无法处理StreamCorruptedException ... o /我得到的是     文件C098.txt已存在     产品编号: - P001     描述: - 预订     价格: - 200卢比     线程“main”中的异常java.io.StreamCorruptedException:类型代码无效:     AC             at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)             at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)             在Utility.getProducts(Utility.java:57)             在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();
            }
        }
    }
}`  

1 个答案:

答案 0 :(得分:0)

你不能'处理'它。你必须阻止它。它是由设计错误导致的,例如在单个ObjectOutputStreams读取的流上使用两个ObjectInputStream,因为您在此处通过附加到文件,或者写入除对象之外的数据而不是读取它是对称的。