我是Java新手,我正在学习序列化和反序列化。我做了一个小的expirement程序来帮助我学习它并掌握它的窍门。我试图保存arrayList的数据,这样我就不必在每次运行程序时都创建一个没有任何信息的完整的新程序。
如果我在程序运行时修改了一个arrayList,在arrayList中添加或删除,我是否必须重新序列化整个arrayList?或者是否有一种方法可以简单地序列化列表中的对象?
这是我的小演示项目,如果你需要它:
按钮及其操作
添加说明
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.");
}
从arrayList中删除项
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
谢谢你的任何帮助:)
1条答案
按热度按时间ajsxfq5m1#
根据您的问题,您不是在序列化对象,而是在序列化“列表”。
因此,你必须重写(序列化)整个列表的每一个变化。
Java序列化是一个关于长期存储(版本控制)和反序列化安全攻击的已知问题。
考虑使用JPA和某种数据库,还有“内存中”和“本地”数据库,它们非常适合学习数据存储和检索。