【问题标题】:Serializing an object to file creates blank object in file将对象序列化为文件会在文件中创建空白对象
【发布时间】:2017-12-12 07:15:10
【问题描述】:

我正在尝试创建一个对象,然后将其序列化并写入文件,但无论我尝试什么,总是将空白对象写入文件。

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
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;

public class FileIO implements Serializable {

    private static final long serialVersionUID = 1L;
    private VIAModel viaModel1;
    private VIAView viaView1 = new VIAView();
    private VIAController viaContr = new VIAController();

    public void setVIAModelFromFile() throws IOException, ClassNotFoundException, EOFException {
        boolean endOfFile = false;
        FileInputStream fstream = new FileInputStream("viaModel.ser");
        ObjectInputStream inputFile = new ObjectInputStream(fstream);

        while (!endOfFile) {
            try {
                viaModel1 = (VIAModel) inputFile.readObject();
            } catch (EOFException eof) {
                endOfFile = true;
            }
        }
        inputFile.close();
    }

    public void setToFile() throws IOException {
        viaContr = viaView1.getController();
        viaModel1.setEventList(viaContr.getVIAMod().getEventList());
        System.out.println(viaModel1.getEventList().getListOfEvents());
        FileOutputStream fstream = new FileOutputStream("viaModel.ser");
        ObjectOutputStream outputFile = new ObjectOutputStream(fstream);

        try {
            outputFile.writeObject(viaModel1);
            outputFile.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found.");
        } catch (IOException ioe) {
            System.out.println("Error.");
            ioe.printStackTrace();
        }
    }

    public VIAModel getVIAModel() {
        return viaModel1;
    }

    public void setVIAModel(VIAModel viamod) {
        this.viaModel1 = viamod;
    }

}

正在写入的对象在里面的所有对象上都可以序列化,无法序列化的对象已经手动序列化。 system.out.print 显示带有程序中输入信息的对象,但此信息根本不会出现在 .ser 文件中,因此稍后仅读取一个空白对象。

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javafx.beans.property.SimpleStringProperty;

public class Events implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 5596571541918537611L;
private transient SimpleStringProperty name = new SimpleStringProperty("");
private transient SimpleStringProperty date = new SimpleStringProperty("");
private transient SimpleStringProperty duration = new SimpleStringProperty("");
private transient SimpleStringProperty type = new SimpleStringProperty("");
private transient SimpleStringProperty location = new SimpleStringProperty("");
private transient SimpleStringProperty category = new SimpleStringProperty("");
// private Lecturer conductor;
private transient SimpleStringProperty price = new SimpleStringProperty("");
private transient SimpleStringProperty minPartic = new SimpleStringProperty("");
private transient SimpleStringProperty maxPartic = new SimpleStringProperty("");
private boolean isFinalized = false;
// ArrayList<Members> eventMembList = new ArrayList<>();

public Events(String name, String date, String duration, String type, String location, String category,
        /* Lecturer conductor, */ String price, String minPartic, String maxPartic, boolean isFinalized) {
    setName(name);
    setDate(date);
    setDuration(duration);
    setType(type);
    setLocation(location);
    setCategory(category);
    setPrice(price);
    setMinPartic(minPartic);
    setMaxPartic(maxPartic);
    this.isFinalized = isFinalized;
}

public Events() {
    this("","","","","","","","","",false);
}

public String getName() {
    return name.get();
}

public void setName(String name) {
    this.name.set(name);
}

public String getDate() {
    return date.get();
}

public void setDate(String date) {
    this.date.set(date);
}

public String getDuration() {
    return duration.get();
}

public void setDuration(String duration) {
    this.duration.set(duration);
}

public String getType() {
    return type.get();
}

public void setType(String type) {
    this.type.set(type);
}

public String getLocation() {
    return location.get();
}

public void setLocation(String location) {
    this.location.set(location);
}

public String getCategory() {
    return category.get();
}

public void setCategory(String category) {
    this.category.set(category);
}

public String getPrice() {
    return price.get();
}

public void setPrice(String price) {
    this.price.set(price);
}

public String getMinPartic() {
    return minPartic.get();
}

public void setMinPartic(String minPartic) {
    this.minPartic.set(minPartic);
}

public String getMaxPartic() {
    return maxPartic.get();
}

public void setMaxPartic(String maxPartic) {
    this.maxPartic.set(maxPartic);
}

public boolean isFinalized() {
    return isFinalized;
}

public void setFinalized(boolean isFinalized) {
    this.isFinalized = isFinalized;
}

public void finalizeEvent() {
    this.isFinalized = true;
}
// public void addMemToEvent(Members member) {
// eventMembList.add(member);
// }
public String toString() {
    return this.name + "\n" +   this.date+ "\n" + this.duration+ "\n" + this.type+ "\n" +   this.location+ "\n" +   this.category+ "\n" + this.price+ "\n" + this.minPartic+ "\n" + this.maxPartic+ "\n" + this.isFinalized;
}

public void readExternal(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    name = new SimpleStringProperty((String) in.readObject());
    date = new SimpleStringProperty((String) in.readObject());
    duration = new SimpleStringProperty((String) in.readObject());
    type = new SimpleStringProperty((String) in.readObject());
    location = new SimpleStringProperty((String) in.readObject());
    category = new SimpleStringProperty((String) in.readObject());
    price = new SimpleStringProperty((String) in.readObject());
    minPartic = new SimpleStringProperty((String) in.readObject());
    maxPartic = new SimpleStringProperty((String) in.readObject());
}

public void writeExternal(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    out.writeObject(name.get());
    out.writeObject(date.get());
    out.writeObject(duration.get());
    out.writeObject(type.get());
    out.writeObject(location.get());
    out.writeObject(category.get());
    out.writeObject(price.get());
    out.writeObject(minPartic.get());
    out.writeObject(maxPartic.get());

}

}

【问题讨论】:

  • Define '信息根本没有出现在.ser文件中'。
  • 我很抱歉。 .ser 文件显示一个空白对象,其中没有写入 viaModel1 对象中的内容。
  • 如何显示? ViaModelViaViewVIAController的定义在哪里?
  • 如果不发布整个项目很难解释,我听说这是不赞成的。本质上,viaModel 是由一个 eventsList 对象、一个 memberList 对象和一个 LecturerList 对象组成的。其中的每一个都存储了许多事件、成员和讲师对象。在我的控制器中,这些是通过单击按钮并将字符串作为属性添加到对象中创建的(希望这是有道理的)。这里的 viaModel 包含这些对象中的每一个,但是当我从文件中读取 viaModel 时,它列出了一个空的 viaModel。
  • 不接受在这里发布部分问题,或者您认为代码的作用。例如,完全有可能我提到的所有类都没有非瞬态非静态字段,在这种情况下,您的期望是 Olympian。

标签: java serialization java-io


【解决方案1】:

将 SimpleStringProperty 更改为 String 似乎工作得很好,并且消除了与序列化有关的所有问题,这是我不知道要纠正的问题。

【讨论】:

    【解决方案2】:

    在您下面的代码中,您最终将只读取最后一个对象,确保您正在从输入文件中读取正确的内容。您是否尝试填充一个新的VIAModel 对象,然后将其写入文件

     while (!endOfFile) {
            try {
                viaModel1 = (VIAModel) inputFile.readObject();
            } catch (EOFException eof) {
                endOfFile = true;
        }
    

    【讨论】:

    • 故意设置只有 1 个对象被写入文件,1 个对象从文件中读取。所有其他对象都包含在此对象中。我做的第一件事是在文件中创建一个空白对象,它在开始时被读取。可以编辑对象,然后在程序关闭之前将其保存到文件中。上面代码中 viaModel1 的 system.out.print 显示了任何添加的对象,但由于某种原因,这些对象没有输出到文件中。
    • 我猜没有真正的原因。我现在将删除循环。
    【解决方案3】:

    正如我所说。你有

    public class Events implements Serializable
    

    还有一系列的transient字段,还有

    public void readExternal(ObjectInputStream in)
    

    public void writeExternal(ObjectOutputStream out)
    

    这些方法永远不会被调用。对象序列化规范中没有关于这些方法签名的任何内容。

    如果你希望这个类被序列化,你需要要么完全删除transient,如果SimpleStringPropertySerializable,并删除这些方法,或者改成extends Externalizable,并修复产生的编译错误。

    不能做的只是自己构建语义和签名,然后想知道为什么 Java 不实现它们。

    【讨论】:

    • 我很抱歉,如果其中任何一个似乎无知。我目前正在学习,老实说,我对此非常了解。我按照您的说法实施了更改并出现错误。 guiView.VIAModel; Serializable 与 Externalizable 不兼容
    • 您实施了哪些更改?我给了你替代品。
    • 对不起。只有一种方法是可能的。 SimpleStringProperty 不可序列化,因此删除瞬态将不起作用。我尝试了第二种方法并得到了那个错误。然后我意识到我可以使用 Serializable 中的 readObject 和 writeObject 而不必使用 Externalizable。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2012-01-08
    • 2015-02-23
    • 2011-04-09
    相关资源
    最近更新 更多