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

3 回答 3

1

这称为附加;您需要以附加模式打开输出文件。

请参阅如何在 Java 中向现有文件添加新的文本行?.

于 2013-08-11T12:02:36.570 回答
0

试试这个在你的数据之后添加一个新行:

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

如果您想在数据之前添加新行,请尝试以下操作:

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

如果要添加多行,
对于 2 行,将 "\n" 替换为 "\n\n"
对于 3 行,将 "\n" 替换为 "\n\n\n"
等等。

于 2017-02-19T07:13:22.717 回答
0

您必须更改为追加并添加新行,如下所示:

    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");
    }
于 2021-06-30T16:46:49.900 回答