2013-01-18 32 views
0

`我是java新手,在下面的代碼中得到StreamCorruptedException ...在這段代碼中,我試圖用ObjectInputStream從文件中讀取多個對象... m無法處理StreamCorruptedException ... o/p我m到處是 文件C098.txt已經存在 產品編號: - P001 說明: - 圖書 價格: - Rs.200 異常線程 「main」 java.io.StreamCorruptedException:無效類型代碼: AC 在java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374) at Utility.main(Utility。)Utility.getProducts(Utility.java:57) java的:23)如何在使用ObjectInputStream時處理StreamCorruptedException?

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(); 
      } 
     } 
    } 
}` 
+0

您不能附加到ObjectOutputStream。你可以用你自己的協議來封裝這樣的流,或者每次重寫整個文件。 –

+0

我的第一條評論是不正確的。我修好了它。 –

+0

好的,謝謝彼得。 – Lizzie

回答

0

的問題是,因爲頭問題的,你是附加到相同的文件,並在返回第二個對象,它拋出,因爲頭問題除外。嘗試在不同的文件中寫入對象,可以擺脫這個問題。

SCE從對象流讀取的控制信息違反內部一致性檢查時發生。 嘗試

 
    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>
+0

所有'exists/createNewFile()'東西都是時間和空間的完全浪費。如果需要,新的FileOutputStream()將會創建一個新文件。你迫使系統測試兩次並創建兩次。 – EJP

0

你不能'處理'它。你必須防止它。它是由設計錯誤產生的,例如在單個ObjectInputStream讀取的數據流上使用兩個ObjectOutputStreams,這是因爲您正在通過附加到該文件或寫入除對象以外的數據而不是對稱讀取數據。

相關問題