如何反序列化该数据并将其设置为 javafx 中的文本字段?
How to deserialize and set that data to textfield in javafx?
我一直在使用 javafx 开发一个 class 项目,我必须序列化所有数据并在项目启动时将所有数据加载到项目中。我能够序列化文本字段中的数据,但无法将序列化数据加载到文本字段中。
这是我的序列化代码:
public class Controller {
Employee nameFile = new Employee();
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
public void getinfo() throws FileNotFoundException, IOException {
String filename = "file.abc";
nameFile.setFirstNameField(firstNameField.getText());
nameFile.setLastNameField(lastNameField.getText());
// Serialization
try
{
//Saving of object in a file
File employeeName = new File("file.abc");
FileOutputStream file = new FileOutputStream(employeeName);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(nameFile);
out.close();
file.close();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Saved");
alert.setHeaderText(null);
alert.setContentText("Your Information has been saved!");
alert.showAndWait();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
}
public void setDataField() {
try {
// Reading the object from a file
FileInputStream file = new FileInputStream("file.abc");
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
nameFile = (Employee) in.readObject();
/*How to I pull that data to the textfield firstnamefield and lastnamefield this method is invoked */
in.close();
file.close();
} catch (FileNotFoundException ex) {
System.out.println("FileNotFoundException is caught");
} catch (IOException ex) {
System.out.println("IOException is caught");
}
}
}
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
nameFile = (Employee) in.readObject();
// *** Here we set data back to text fields ***
firstNameField.setText(nameFile.getFirstNameField());
lastNameField.setText(nameFile.getLastNameField());
我一直在使用 javafx 开发一个 class 项目,我必须序列化所有数据并在项目启动时将所有数据加载到项目中。我能够序列化文本字段中的数据,但无法将序列化数据加载到文本字段中。 这是我的序列化代码:
public class Controller {
Employee nameFile = new Employee();
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
public void getinfo() throws FileNotFoundException, IOException {
String filename = "file.abc";
nameFile.setFirstNameField(firstNameField.getText());
nameFile.setLastNameField(lastNameField.getText());
// Serialization
try
{
//Saving of object in a file
File employeeName = new File("file.abc");
FileOutputStream file = new FileOutputStream(employeeName);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(nameFile);
out.close();
file.close();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Information Saved");
alert.setHeaderText(null);
alert.setContentText("Your Information has been saved!");
alert.showAndWait();
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
}
public void setDataField() {
try {
// Reading the object from a file
FileInputStream file = new FileInputStream("file.abc");
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
nameFile = (Employee) in.readObject();
/*How to I pull that data to the textfield firstnamefield and lastnamefield this method is invoked */
in.close();
file.close();
} catch (FileNotFoundException ex) {
System.out.println("FileNotFoundException is caught");
} catch (IOException ex) {
System.out.println("IOException is caught");
}
}
}
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
nameFile = (Employee) in.readObject();
// *** Here we set data back to text fields ***
firstNameField.setText(nameFile.getFirstNameField());
lastNameField.setText(nameFile.getLastNameField());