将对象序列化为文件会在文件中创建空白对象

时间:2017-12-12 07:15:10

标签: java serialization java-io

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

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());

}

}

3 个答案:

答案 0 :(得分:1)

将SimpleStringProperty更改为String似乎完美无缺,并且消除了序列化所涉及的所有问题,这是我无法纠正的知识。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

正如我所说的那样。

public class Events implements Serializable

以及一系列transient字段,以及

public void readExternal(ObjectInputStream in)

public void writeExternal(ObjectOutputStream out)

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

如果您希望序列化此类,则需要始终删除transient,如果SimpleStringPropertySerializable,则删除这些方法,或者extends Externalizable ,并修复导致的编译错误。

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