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.