如何序列化上面有 ObservableList 的对象?
How to serialize an object that has an ObservableList on it?
我实际上想序列化一个对象列表,但我认为这没有区别。
我正在放置只有两个属性的 class。一个字符串和一个 ObservableList。我将在其中包含序列化方法。我知道 ObservableList 没有实现可序列化,但我用它们填充了一个 TableView,这就是我使用它的原因,但如果有其他方法,我愿意接受。
public class Tratamiento implements Serializable{
private String name;
private ObservableList<Material> materials;
public Tratamiento(String name, ObservableList<Material> materials) {
this.name= name;
this.materials= materials;
}
public Treatment(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
public ObservableList getMaterials() {
return materials;
}
public void setMaterials(ObservableList materials) {
this.materials = materials;
}
public int addTreatment(List tratamiento) {
int res = 0; //Failure
try {
FileOutputStream fileOut = new FileOutputStream("Treatment.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(treatment);
out.close();
fileOut.close();
System.out.printf("Object has been serialized");
res = 1;
} catch (IOException i) {
i.printStackTrace();
}
return res;
}
public List consultListTreatments() {
int res = 0;
//Deserialize file
List<Treatment> list = new ArrayList<Treatment>();
try {
FileInputStream fileIn = new FileInputStream("Treatment.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
list = (List) in.readObject();
in.close();
fileIn.close();
res = 1;
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Class not found");
c.printStackTrace();
}
return list;
}
}
这是我的 FXML 控制器中的实现
@FXML
private void createTreatment(ActionEvent event){
Treatment tr = new Treatment();
List<Treatment> list = new ArrayList<Treatment>();
list = tr.consultListTreatments();
ObservableList<Material> rows = table.getItems();//FXCollections.observableArrayList();
//get values of the rows
Treatament newTreatment = new Treatment(TextFieldTreatmentName.getText(),rows);
list.add(newTreatment);
newTreatment.addTreatment(list);
TextFieldTreatmentName.clear();
}
我看到了这个问题,但它不符合我的需要,我也无法将其修改为我想要的。
在这种情况下,您将必须实现 Externalizable
接口。这将要求您编写自己的方法来序列化和反序列化您的对象。
我实际上想序列化一个对象列表,但我认为这没有区别。 我正在放置只有两个属性的 class。一个字符串和一个 ObservableList。我将在其中包含序列化方法。我知道 ObservableList 没有实现可序列化,但我用它们填充了一个 TableView,这就是我使用它的原因,但如果有其他方法,我愿意接受。
public class Tratamiento implements Serializable{
private String name;
private ObservableList<Material> materials;
public Tratamiento(String name, ObservableList<Material> materials) {
this.name= name;
this.materials= materials;
}
public Treatment(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
public ObservableList getMaterials() {
return materials;
}
public void setMaterials(ObservableList materials) {
this.materials = materials;
}
public int addTreatment(List tratamiento) {
int res = 0; //Failure
try {
FileOutputStream fileOut = new FileOutputStream("Treatment.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(treatment);
out.close();
fileOut.close();
System.out.printf("Object has been serialized");
res = 1;
} catch (IOException i) {
i.printStackTrace();
}
return res;
}
public List consultListTreatments() {
int res = 0;
//Deserialize file
List<Treatment> list = new ArrayList<Treatment>();
try {
FileInputStream fileIn = new FileInputStream("Treatment.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
list = (List) in.readObject();
in.close();
fileIn.close();
res = 1;
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Class not found");
c.printStackTrace();
}
return list;
}
}
这是我的 FXML 控制器中的实现
@FXML
private void createTreatment(ActionEvent event){
Treatment tr = new Treatment();
List<Treatment> list = new ArrayList<Treatment>();
list = tr.consultListTreatments();
ObservableList<Material> rows = table.getItems();//FXCollections.observableArrayList();
//get values of the rows
Treatament newTreatment = new Treatment(TextFieldTreatmentName.getText(),rows);
list.add(newTreatment);
newTreatment.addTreatment(list);
TextFieldTreatmentName.clear();
}
我看到了这个问题,但它不符合我的需要,我也无法将其修改为我想要的。
在这种情况下,您将必须实现 Externalizable
接口。这将要求您编写自己的方法来序列化和反序列化您的对象。