我是Java新手,我正在学习序列化和反序列化。我做了一个小实验程序来帮助我学习和掌握它的窍门。我试图保存数组列表的数据,这样我就不必每次运行程序时都创建一个没有信息的完整的新数组列表。
如果我要在程序运行时通过添加或删除数组列表来修改数组列表,我是否需要重新序列化整个数组列表?或者是否有一种方法可以简单地序列化列表中的对象?
如果你需要,这里是我的小演示项目:
按钮和它们的动作
添加指令
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
JobTypeTxt.setText("");
salaryTxt.setText("");
companyTxt.setText("");
JobTypeTxt.setEditable(true);
salaryTxt.setEditable(true);
companyTxt.setEditable(true);
InstructionsTxt.setText("The textfields above have now become editable. Type in your new job's type, salary, and company, and then click save to add it to the list.");
}
删除数组列表中的项
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
if(jobList.getSelectedValue() == null) {
InstructionsTxt.setText("Make sure you select an object in the list that you want to delete.");
} else {
Data.getJobs().remove(jobList.getSelectedIndex());
JobTypeTxt.setText("");
salaryTxt.setText("");
companyTxt.setText("");
jobList.revalidate();
jobList.repaint();
}
}
保存并实际添加到arrayList
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
try{
double salary = Double.parseDouble(salaryTxt.getText().trim());
Job job = new Job(JobTypeTxt.getText().trim(), salary, companyTxt.getText().trim());
Data.getJobs().add(job);
JobTypeTxt.setEditable(false);
salaryTxt.setEditable(false);
companyTxt.setEditable(false);
InstructionsTxt.setText("");
} catch(NumberFormatException e) {
InstructionsTxt.setText("The salary must be a valid decimal!");
}
jobList.revalidate();
jobList.repaint();
}
变量声明
// Variables declaration - do not modify
private javax.swing.JTextArea InstructionsTxt;
private javax.swing.JTextField JobTypeTxt;
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnSave;
private javax.swing.JTextField companyTxt;
private javax.swing.JScrollPane jScrollPane;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList<String> jobList;
private javax.swing.JLabel lblCompany;
private javax.swing.JLabel lblJobType;
private javax.swing.JLabel lblSalary;
private javax.swing.JTextField salaryTxt;
// End of variables declaration
}
类工作
package deserialization.and.serialization;
public class Job {
private String type;
private double salaryPerHr;
private String company;
public Job(String type, double salary, String company) {
this.type = type;
salaryPerHr = salary;
this.company = company;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getSalary() {
return salaryPerHr;
}
public void setSalary(double salary) {
salaryPerHr = salary;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
类数据package deserialization.and.serialization;
import java.util.ArrayList;
public class Data {
private static ArrayList<Job> jobs = new ArrayList<Job>();
public static ArrayList<Job> getJobs() {
return jobs;
}
public static void setJobs(ArrayList<Job> allJobs) {
jobs = allJobs;
}
}
GUI的外观
GUI
谢谢你的任何帮助:)
根据您的问题,您不是序列化对象,而是序列化"列表"。
因此,每次更改都必须重写(序列化)整个List。
Java序列化是一个关于长期存储(版本控制)和针对类装入器的反序列化安全攻击的已知问题。
考虑使用JPA和某种数据库,也有内存"one_answers";local"对于学习数据存储和检索非常有用的数据库。