从FileOutputStream读取数据会为我的数据添加“null”

时间:2013-06-06 23:35:17

标签: android fileinputstream fileoutputstream

在我的第一个Activity我正在FileOutputStream中保存坐标,然后在另一个Activity中我读取数据。但它总是在我的协调中添加“null”。我做错了吗?

活动1我将数据写入FileOutPutStream

FileOutputStream fos;
    try {
        String lat, lng;
        lat = String.valueOf(location_latitude);
        lng = String.valueOf(location_longitude);

        fos = openFileOutput("my_latitude", Context.MODE_PRIVATE);
        fos.write(lat.getBytes());
        fos.close();

        fos = openFileOutput("my_longitude", Context.MODE_PRIVATE);
        fos.write(lng.getBytes());
        fos.close();
        Log.e("SPLASHER",lat);
    } catch (Exception e) {
        e.printStackTrace();
     }

我阅读数据的活动2

private void getLocations() {
        String[] locations = getApplicationContext().fileList();
        for (int i = 0; i < locations.length; i++) {

            FileInputStream fis;

            try {
                fis = openFileInput(locations[i]);
                byte[] input = new byte[fis.available()];

                if (locations[0].equals("my_latitude")) {

                while (fis.read(input) != -1) {
                    myLat += new String(input);
                    // int start = myLat.indexOf("null");
                    // String suffix = myLat.substring(start);
                    myLat.replaceAll(".*?null", "");
                    Log.e("READING", myLat);
                    }
                }
                fis.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // Log.e("LOCATTTIEEEE", locations[i]);

        }
    }

日志

06-07 01:28:10.565: E/SPLASHER(9769): 51.1878003
06-07 01:25:41.660: E/YESSIR(6359): null51.1878167

所以我认为写作没问题,但阅读部分出了什么问题?

1 个答案:

答案 0 :(得分:1)

我发现了错误..

我必须像String lat, lng;

那样初始化String lat="", lng="";

这解决了我的问题!!