Android: Saving data to files

3

I'm having trouble using files on android.

Save the file and use it quietly to close the application, when I open the app again the file is recreated and I lose the recorded data.

I tested several solutions, I have this code now:

public class PersistenciaSerial {
    String arquivo;
    Context cont;
    File fileDir;

    public PersistenciaSerial(Context context, String fileName) {
        cont = context;
        fileDir = new File(Environment.getExternalStorageDirectory().getPath()+"/saude");
        if(fileDir.isDirectory()){
            fileDir.mkdirs();
        } else {
            Log.e("Diretório já existente", fileDir.getName());
        }
        arquivo = fileDir+"/"+fileName;
    }

    public Object pegarObjeto() {
        Object lista = null;
        //Classe responsavel por recuperar os objetos do arquivo
        try {
            FileInputStream fileInputStream = cont.openFileInput(arquivo);
            Log.e("Teste de fileinputstream", fileInputStream.toString());
            ObjectInputStream objLeitura = new ObjectInputStream(fileInputStream);
            lista = (Object)objLeitura.readObject();
            objLeitura.close();
            objLeitura.close();
            Log.e("Teste OFF Pegando", String.valueOf(lista));
        }catch (Exception e){
            Log.e("Teste Pegando False", e.toString());
        }
        return lista;
    }

    public void persistir(Object listaEntrada) {
        //Classe responsavel por inserir os objetos
        try {
            Log.e("Teste OFF Persistindo", "Persistindo");
            FileOutputStream fileOutputStream = cont.openFileOutput(arquivo, 1);
            Log.e("Teste de fileoutputstream", fileOutputStream.toString());
            ObjectOutputStream objGravar = new ObjectOutputStream(fileOutputStream);
            //Grava o objeto cliente no arquivo
            objGravar.writeObject(listaEntrada);
            objGravar.flush();
            Log.e("Teste OFF Persistindo", arquivo);
            Log.e("Teste OFF Persistindo", String.valueOf(listaEntrada));
        }catch (Exception e){
            Log.e("Teste Persistindo False", e.toString());
        }
    }
}

I put the whole class out of desperation.

I thank you for your time dedicated to my doubt.

    
asked by anonymous 04.01.2016 / 13:55

2 answers

2

Use BufferedWriter with FileWriter where the second parameter is true to append to it ( FileWriter ):

BufferedWriter writer = new BufferedWriter(new FileWriter(arquivo, true));
writer.write("texto");
writer.flush();
writer.close();

Note that BufferedWriter also has append method which may be clearer in some cases.

    
04.01.2016 / 15:09
1

In order to be able to append data to an open file with openFileOutput(String name, int mode) the value for the mode parameter should be Context.MODE_APPEND

Change the line

FileOutputStream fileOutputStream = cont.openFileOutput(arquivo, 1);

for

FileOutputStream fileOutputStream = cont.openFileOutput(arquivo, Context.MODE_APPEND);
    
07.01.2016 / 19:55