다음 개체를 만들어 직렬화하고 파일에 쓰려고하지만 시도한 것과 관계없이 빈 개체가 항상 파일에 쓰여집니다.파일에 개체를 serialize하면 파일에 빈 개체가 생성됩니다.
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());
}
}
'.ser 파일에 정보가 전혀 표시되지 않음'을 정의하십시오. – EJP
사과드립니다. .ser 파일은 작성된 viaModel1 개체에 있던 내용이없는 빈 개체를 표시합니다. –
어떻게 표시 되나요? 그리고'ViaModel','ViaView', 그리고'VIAController'의 정의는 어디에 있습니까? – EJP