ObjectOutputStream没有写入文件

时间:2016-09-22 15:15:36

标签: java 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");
}

..但是抛出了IOException。注释掉的代码确实设法创建文件,但它是空的,所以我认为它不是权限问题?经过一些阅读后,我发现了ObjectOutputStream但不会写入该文件。我做错了什么?

2 个答案:

答案 0 :(得分:0)

确认您的班级交易实施Serializable,您可能会遇到类型例外java.io.NotSerializableException

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

public class Transaction implements Serializable{...}

答案 1 :(得分: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 + "]";
    }

}

这可能对你有帮助!