【问题标题】:ObjectOutputStream not writing to fileObjectOutputStream 未写入文件
【发布时间】:2016-09-23 01:16:06
【问题描述】:

我正在尝试使用以下内容将对象序列化到文件中:

// 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");
}

..但是 IOException 被抛出。被注释掉的代码确实可以创建文件,但它是空的,所以我认为这不是权限问题?经过一番阅读,我找到了 ObjectOutputStream 但不会写入文件。我做错了什么?

【问题讨论】:

  • 好吧,您目前正在处理异常,只是打印IOException。打印出异常本身,它会告诉你出了什么问题。请注意,您还应该使用 try-with-resources 来处理关闭...
  • 使用e.printStackTrace() 代替System.out.println("IOException")。请edit 问题并添加生成的堆栈跟踪,格式正确。当您有异常时,堆栈跟踪是首先要查看的地方 - 不要用无意义的打印来抑制它。

标签: java objectoutputstream


【解决方案1】:

验证你的类 Transaction 实现了Serializable,你可能有一个类型异常java.io.NotSerializableException

我尝试了你的代码,我得出的错误是它没有实现Serializable 接口,因为没有它可能无法将你的对象转换为字节,然后将它们写入文件

public class Transaction implements Serializable{...}

【讨论】:

  • 虽然这可能是 OP 问题的原因,但请不要发布猜测作为答案(如果需要提供正确答案,我们有 cmets 部分向 OP 询问更多信息)。如果不是猜测,请说明为什么您确定这是问题所在,以便 OP 和其他人可以学习如何分析这种情况。
  • 良好的观察,考虑
【解决方案2】:
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 + "]";
    }

}

这可能对你有帮助!

【讨论】:

    猜你喜欢
    • 2016-05-31
    • 2019-09-13
    • 1970-01-01
    • 2016-07-25
    • 2014-04-18
    • 2017-10-02
    • 2013-12-14
    • 2016-08-01
    • 2017-04-10
    相关资源
    最近更新 更多