1

I have this problem: I want to add an ArrayList object to a file "clientes.dat" in order to store information on that file and display it later. However, whenever I write to said file again, the new ArrayList object overwrites the first one.

Here's the code:

import java.io.*;
import java.util.ArrayList;

public class Archivo implements Serializable{

    private static final long serialVersionUID = 1L;

    public static void nuevoCliente(Cliente cliente){
        Cliente wo=cliente;
        File f = new File("clientes.dat");
        ArrayList<Cliente> woi=new ArrayList<>();
        try {
            FileOutputStream fop=new FileOutputStream(f);
            ObjectOutputStream oos=new ObjectOutputStream(fop);
            woi.add(wo);
            oos.writeObject(woi);
            oos.close();

        } catch (IOException e) {
            System.out.println("El Cliente no pudo ser agregado.");
        }
    }

    @SuppressWarnings("unchecked")
    public static void leerCliente() throws IOException, ClassNotFoundException{
         try {
             FileInputStream fis=new FileInputStream("clientes.dat");
             ObjectInputStream ois=new ObjectInputStream(fis);
             ArrayList<Cliente> woi=new ArrayList<>();
             woi=(ArrayList<Cliente>)ois.readObject();

             for(int i=0;i<woi.size();i++){
                 System.out.println(woi.get(i).getNombre());
             }  

             ois.close();

         }catch(IOException e){
             System.out.println("El archivo no se pudo leer");
         }

    }
}

I have tried using FileOutputStream fop=new FileOutputStream(f, true); But if I do it that way, the leerCliente() method will only return the first ArrayList that was stored, not the new one.

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
  • Pass true as a second argument to FileWriter to turn on "append" mode. fout = new FileWriter("filename.txt", true); – akshaya pandey Nov 16 '17 at 06:30
  • `But if I do it that way, the leerCliente() method will only return the first ArrayList that was stored, not the new one` - have you tried calling `ois.readObject()` multiple times in a loop, to read all the ArrayLists stored in the file? – Eran Nov 16 '17 at 06:32
  • @Eran Sorry for my ignorance, but how exactly would I do it? – O. Villarreal Nov 16 '17 at 06:35
  • @O.Villarreal I'd try something like `while `(woi=(ArrayList)ois.readObject()) != null) {...}` – Eran Nov 16 '17 at 06:37
  • Can you store each object in different file maybe? You can add a random number (And make sure that the file does not exist ) and have separate files for each object. I am not sure how are you planning to retrieve multiple objects from files if you don`t want to store some info alongside showing how many bytes each object should have. – Saik Nov 16 '17 at 06:37
  • Read the old `ArrayList` from the file if it exists, add the new `Cliente`, write the updated `ArrayList` to the file, overwriting it. Do not use append mode on object output stream files: it does not work without special measures @akshayapandey . – user207421 Nov 16 '17 at 07:08

0 Answers0