Search code examples
androidfiletextgpsmultiline

Write a multiline .txt file in Android


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");
        }

Solution

  • 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?.