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

"the other method doesn't work"....
1. read the file's lines into a resultset
2. while line != null -> line = resultset.nextLine(); add line into arraylist

this is the logic for reading

start with the arraylist, to which you've made the changes
write it to the txt file with the same name, don't append it.

In your load methods you read the file into a local arraylist then return. The arraylist is then discarded. You pass a parameter, but you never use that either. You should return the arraylist from the method and not have a void return type. Eg

public ArrayList<Object> LoadData(){
   ...
    ArrayList<Object> p1 =  ...
   ...
   return p1;
}

ps ObjectOutputStream writes a binary file - it's not text.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.