0

I am learning how to develop in Android and I have a question, I have a GPS that locates you, then it writes the longitude and the latitude in a .txt file, when the position change, it writes again the new position BUT it replaces the file, I would like to know how to write the new location BEHIND the other.

For example:

Latitude: 42.56164
Latitude: 42.52542
Latitude: 42.58864
Latitude: 42.54422

   try
        {
            OutputStreamWriter fout= new OutputStreamWriter(openFileOutput("datosgpslongitud.txt", Context.MODE_PRIVATE));

            //String gps = ((EditText)findViewById(R.id.aa)).getText().toString();

            String linea = System.getProperty("line.separator");

            fout.write(linea+longitude);
            fout.close();
        }
        catch (Exception ex)
        {
            Log.e("Ficheros", "Error al escribir fichero a memoria interna");
        }
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user2667879
  • 45
  • 2
  • 8

3 Answers3

1

This is called appending; you need to open the output file in append mode.

See How to add a new line of text to an existing file in Java?.

Community
  • 1
  • 1
andy256
  • 2,821
  • 2
  • 13
  • 19
0

Try this to add a new line after your data:

String data=yourEditText.getText.tostring();
data+="\n";
fout.write(data);

and try this if you want to add a new line before your data:

String data=yourEditText.getText.tostring();
data="\n"+data;
fout.write(data);

If you want to add multiple lines,
for 2 lines, replace "\n" with "\n\n"
for 3 lines, replace "\n" with "\n\n\n"
and so on.

Pang
  • 9,564
  • 146
  • 81
  • 122
mohamad
  • 1
  • 2
0

You have to change to append and add new line, like this :

    try
    {
        OutputStreamWriter fout= new OutputStreamWriter(openFileOutput("datosgpslongitud.txt", Context.MODE_APPEND));

        //String gps = ((EditText)findViewById(R.id.aa)).getText().toString();

        String linea = System.getProperty("line.separator");

        fout.write(linea+longitude+"\n");
        fout.close();
    }
    catch (Exception ex)
    {
        Log.e("Ficheros", "Error al escribir fichero a memoria interna");
    }