0

`我是java新手,在下面的代码中得到StreamCorruptedException......在这段代码中,我试图使用ObjectInputStream从一个文件中读取多个对象......我无法处理StreamCorruptedException......我的o / p得到的是文件 C098.txt 已经存在 产品 ID:- P001 描述:- 书价:- Rs.200 线程“main”中的异常 java.io.StreamCorruptedException:无效类型代码:AC at java.io.ObjectInputStream.readObject0(ObjectInputStream .java:1374) 在 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();
            }
        }
    }
}`  
4

2 回答 2

0

你不能“处理”它。你必须阻止它。它是由设计错误引起的,例如ObjectOutputStreams在单个 读取的流上使用两个ObjectInputStream,就像您在此处通过附加到文件或写入对象以外的数据而不是对称读取它所做的那样。

于 2013-01-18T23:57:57.673 回答
0

问题是由于标题问题,您正在附加到同一个文件,并且在返回第二个对象时由于标题问题而引发异常。尝试在不同的文件中写入对象,您可以摆脱问题。

SCE 当从对象流中读取的控制信息违反内部一致性检查时引发。尝试

    导入java.io.*;
    导入 java.util.*;
    类产品实现可序列化{
        私有静态最终长序列版本UID = 1L;
        字符串产品ID;
        字符串描述;
        字符串价格;
        公共产品(字符串 PId,字符串 a_des,字符串 a_price){
            产品ID=PId;
            desc=a_des;
            价格=a_price;
        }
        公共字符串 toString(){
            return "产品 ID:- "+productId+"\n描述:- "+desc+"\n价格:- "+price;
        }
     // 产品 objProduct;
        公共静态 void main(String args[]) 抛出异常{
            字符串 cartId = "C0982.txt";
            产品 objProduct = new Product("P001","Book","Rs.200");
            addProductToCart(cartId,objProduct);
            获取产品(cartId);
            产品 objProduct1 = new Product("P087","Laptop","Rs.45,500");
            addProductToCart("C0981.txt",objProduct1);
            getProducts("C0981.txt");
        }
        公共静态 void addProductToCart(String CId,Product p) 抛出异常{
            尝试{
            布尔搜索文件;
            文件 objFile = 新文件(CId);
            searchFile = objFile.exists();
            如果(搜索文件)
                System.out.println("文件 "+CId+" 已经存在");
            别的{
                objFile.createNewFile();
                System.out.println("文件 "+CId+" 不存在,现在已创建");
            }
            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>
于 2013-01-18T12:31:57.607 回答