I am new to Java, and I am learning about serialization and deserialization. I made a little expirement program to help me learn it and get the hang of it. I am trying to save the data of an arrayList so I don't have to make a complete new one with no info in it every time I run the program.
If I were to modify an arrayList while a program was running by adding or deleting from the arrayList, would I have to reserialize the WHOLE arrayList? Or would there be a way to just simply serialize the object that is apart of the list?
Here is my little demo project if you need it:
Buttons and their actions
Adding Instructions
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.");
}
Deleting items from the 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();
}
}
Saving and actually adding the item to the 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();
}
Variable Declaration
// 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
}
Class Job
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;
}
}
Class Data
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;
}
}
How the GUI looks
Thank you for any help whatsoever :)
You are not, according to your question, serializing the objects but "the list".
As such, you have to re-write (serialize) the entire List with every change.
Java serialization is a known problem regarding long term storage (versioning) and de-serialization security attacks against the class loader.
Consider using JPA and some kind of database, there are also "in memory" and "local" DBs that are great for learning data storage and retrieval.