I have class with two method to read and write to file
and in my GUI jFram
ArrayList<PersonTest> person = new ArrayList<>();
RWData readWrite = new RWData();
DefaultListModel<PersonTest> model;
private void fillList() {
model = new DefaultListModel<>();
for (PersonTest temp : person) {
model.addElement(temp);
}
jList1.setModel(model);
}
private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PersonTest p1 = new PersonTest();
PersonTest p2 = new PersonTest();
String f = "firstName Here";
String l = "LastName Here";
String f1 = "firstName Here 2";
String l1 = "LastName Here 2";
p1.setfName(f);
p1.setLName(l);
p2.setfName(f1);
p2.setLName(l1);
person.add(p1);
person.add(p2);
fillList();
readWrite.saveFile(person); // this method work
}
in my LoadSave class I have these two method
public void LoadDataArray(ArrayList<Object> obj){
try {
boolean check = true;
ObjectInputStream pbjInput = new ObjectInputStream(new FileInputStream("//Desktop/test.txt"));
try {
ArrayList<Object> p1 = (ArrayList<Object>)pbjInput.readObject();
System.out.println(p1);
} catch (ClassNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void LoadData(Object obj){
try {
ObjectInputStream pbjInput = new ObjectInputStream(new FileInputStream("//Desktop/test.txt"));
try {
ArrayList<Object> o1 = (ArrayList<Object>)pbjInput.readObject();
System.out.println("test output" + o1);
} catch (ClassNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
//this work
public void SaveFile(Object o){
try {
ObjectOutputStream objOStram = new ObjectOutputStream(new FileOutputStream("//Desktop/test.txt"));
objOStram.writeObject(o);
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
but the other method doesn't work.. I want to read the data from the text file and use the method fillList to put the data in the JList and then save the changes to the same text file , but I'm not sure how to do it with arrayList ? can you help me.. what is wrong with my code.. did I miss something