将新行与TEXT文件中的最后一行进行比较

时间:2013-08-23 13:41:34

标签: android string gps compare

我有一个使用GPS查找您的位置的应用,然后将该位置写入txt文件

try
        {
            OutputStreamWriter fout= new OutputStreamWriter(openFileOutput("datosgps.txt", Context.MODE_APPEND));
                fout.append( latitude+longitude+"\n");

            Intent intent = new Intent(getApplicationContext(), aviso.class);
            startActivity(intent);

                fout.close();
        }
        catch (Exception ex)
        {
            Log.e("Ficheros", "Error");
        }

在txt文件中我有这个:

 Latitud: 49.650 Longitud: 2.931
 Latitud: 49.655 Longitud: 2.939
 Latitud: 49.656 Longitud: 2.939
 Latitud: 49.657 Longitud: 2.939
 Latitud: 49.657 Longitud: 2.939

一个在另一个之下。

每隔2分钟写一个新位置, 我该怎么做才能检查NEW行是否与最后一行相同?

例如,我想要它阅读

纬度:49.656纵向:2.939

然后写下新位置

纬度:49.657纵向:2.939

检查它们是否相同

如果没有,什么都不做

阅读最后的

纬度:49.657纵向:2.939

和新位置

纬度:49.657纵向:2.939

如果等于做某事

任何建议都将受到赞赏

EDITED

What do you mean.. something like this?

        String newdata = latitude+longitude;
    if((newdata.equals(lastdata){

    //DO SOMETHING
    }
          try
                {


                    OutputStreamWriter fout= new OutputStreamWriter(

openFileOutput("datosgps.txt", Context.MODE_APPEND));
                fout.append( latitude+longitude+"\n");

                fout.close();
    String lastdata = latitude+longitude;

            }
            catch (Exception ex)
            {
                Log.e("Ficheros", "Error");
            }

现在我有一个字符串中的最后一个数据,而在.txt中我有一行如下:

纬度:43.4556经度:34.333

在6秒内,它将写入新数据并且必须进行比较

newdata with lastdata但是第一次运行时,lastadata不存在,然后崩溃。

感谢您的时间!

1 个答案:

答案 0 :(得分:1)

为什么不坚持你写的最后一个值(将它们存储在last_latitudelast_longitude中)然后在决定写入之前检查新值是否与这些值相同?