2016-09-22 94 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 + "]"; 
    } 

} 

这可能对你有帮助!