2016-09-22 40 views
0

我試圖序列化一個對象具有以下到文件:的ObjectOutputStream沒有寫入文件

// fill with some test data 
ArrayList<Transaction> transactions = new ArrayList<>(); 
transactions.add(new Transaction("Internet", "2016-09-20", -28)); 
transactions.add(new Transaction("Groceries", "2016-09-20", -26)); 

//serialize transactions 
try { 
//   File f = new File("transactions.ser"); 
//   OutputStream file = new FileOutputStream(f); 
//   OutputStream buffer = new BufferedOutputStream(file); 
//   ObjectOutput output = new ObjectOutputStream(buffer); 

    File f = new File("transactions.ser"); 
    FileOutputStream fos = new FileOutputStream(f); 
    ObjectOutputStream out = new ObjectOutputStream(fos); 
    out.writeObject(transactions); 
    out.flush(); 
    out.close(); 

    FileInputStream fis = new FileInputStream(f); 
    ObjectInputStream in = new ObjectInputStream(fis); 
    Object o = in.readObject(); 
    System.out.println(o); 

} 
catch(IOException e){ 
    System.out.println("IOException"); 
} 
catch(ClassNotFoundException e) { 
    System.out.println("ClassNotFoundException"); 
} 

..however IOException異常被拋出。被註釋掉的代碼確實可以創建文件,但它是空的,所以我認爲這不是權限問題?經過一些閱讀後,我發現ObjectOutputStream,但不會寫入文件。我究竟做錯了什麼?

+4

那麼你正在吞嚥異常,只是打印'IOException'。打印出異常本身,它會告訴你什麼是錯的。請注意,您還應該使用try-with-resources來處理關閉...... –

+2

而不是'System.out.println(「IOException」)''使用'e.printStackTrace()'。請[編輯]問題並添加正確格式化的結果堆棧跟蹤。堆棧跟蹤是在發生異常時首先要查看的地方 - 不要用無意義的打印壓縮它。 – RealSkeptic

回答

0

驗證類交易實現Serializable,你可能有一個類型的異常java.io.NotSerializableException

我想你的代碼和我的結論是,它並沒有實現Serializable接口的錯誤,因爲沒有這個可能不你的對象轉換爲字節,然後將它們寫入文件

public class Transaction implements Serializable{...} 
+0

雖然這可能是OP問題的原因,但請不要發佈猜測作爲答案(我們有評論部分詢問OP是否需要提供正確答案的更多信息)。如果不是猜測,那麼請澄清爲什麼你確定這是問題,所以OP和其他人可以學習如何分析這種情況。 – Pshemo

+0

好的觀察,考慮 –

0
package com.crone.core; 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

public class Crunchify { 

    public static void main(String[] args) throws ClassNotFoundException { 
     // TODO Auto-generated method stub 

     int i; 
     Item item = new Item(); 
     List<Item> obj; 
     obj = new ArrayList<Item>(); 


     obj.add(new Item("Item1101","ipad",499,1)); 
     obj.add(new Item("Item1102","iphone",599,3)); 
     // Let's serialize an Object 
     try { 
      FileOutputStream fileOut = new FileOutputStream("./Crunchify_Test1.txt"); 
      ObjectOutputStream out = new ObjectOutputStream(fileOut); 
      out.writeObject(obj); 
      out.close(); 
      fileOut.close(); 
      System.out.println("\nSerialization Successful... Checkout your specified output file..\n"); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     // Let's deserialize an Object 
     try { 
      FileInputStream fileIn = new FileInputStream("./Crunchify_Test1.txt"); 
      ObjectInputStream in = new ObjectInputStream(fileIn); 
      System.out.println("Deserialized Data: \n" + in.readObject().toString()); 
      in.close(); 
      fileIn.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

class Item implements Serializable { 

    private String itemID; 
    private String desc; 
    private double cost; 
    private int quantity; 

    public Item() { 
     itemID = ""; 
     desc = ""; 
     cost = 0; 
     quantity = 0; 
    } 

    public Item(String id, String d, double c, int q) { 
     itemID = id; 
     desc = d; 
     cost = c; 
     quantity = q; 

    } 

    /** 
    * @return the itemID 
    */ 
    public String getItemID() { 
     return itemID; 
    } 

    /** 
    * @param itemID 
    *   the itemID to set 
    */ 
    public void setItemID(String itemID) { 
     this.itemID = itemID; 
    } 

    /** 
    * @return the desc 
    */ 
    public String getDesc() { 
     return desc; 
    } 

    /** 
    * @param desc 
    *   the desc to set 
    */ 
    public void setDesc(String desc) { 
     this.desc = desc; 
    } 

    /** 
    * @return the cost 
    */ 
    public double getCost() { 
     return cost; 
    } 

    /** 
    * @param cost 
    *   the cost to set 
    */ 
    public void setCost(double cost) { 
     this.cost = cost; 
    } 

    /** 
    * @return the quantity 
    */ 
    public int getQuantity() { 
     return quantity; 
    } 

    /** 
    * @param quantity 
    *   the quantity to set 
    */ 
    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 

    /* 
    * @see java.lang.Object#toString() 
    */ 
    @Override 
    public String toString() { 
     return "Item [itemID=" + itemID + ", desc=" + desc + ", cost=" + cost + ", quantity=" + quantity + "]"; 
    } 

} 

這可能對你有幫助!