试图读取整个文件只能得到一行

时间:2015-11-29 02:01:11

标签: java android

我试图用Java for Android读取和写入数据。我可以读写,但当我读到文件时,我只能得到第一行。我不明白为什么。

这是我写入文件的代码。

                    try {
                        System.out.println(currentTime);
                        System.out.println(currentLocation);   
                         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(openFileOutput("history", Context.MODE_PRIVATE)));
                         bw.write(currentTransaction + " " + currentTime + " " + currentLocation + "\n");
                         bw.close();
                        }
                    catch (Exception e) {
                         e.printStackTrace();
                         System.out.println("Error in Writing To History File");
                        }

这是我阅读文件的代码。

            BufferedReader in;
            String totalHistory = null;
            StringBuffer stringBuffer = new StringBuffer();
            String strLine;

            try {
                 in = new BufferedReader(new InputStreamReader(openFileInput("history")));
                 //totalHistory = in.readLine();
                 //while ((strLine = in.readLine()) != null){
                //   stringBuffer.append(strLine);
                //   System.out.println("HERES A THING");
                 //    System.out.println(stringBuffer.toString());
                  //   stringBuffer.append("\n");
                //   totalHistory = stringBuffer.toString();


                     //String[] tokens = strLine.split("|");
                     //totalHistory = totalHistory + tokens[0] + " " + tokens[1] + " " + tokens[2] + " " + tokens[4];
                // }
                 totalHistory = in.readLine();
                 System.out.println(stringBuffer.toString());
                 in.close();
                }
            catch (Exception e) {
                 e.printStackTrace();
                 System.out.println("Error in Reading History.");
                }

正如您所看到的,我在那里有很多注释掉的代码。我已经尝试了很多东西,所以我想把它留在那里,这样我就可以告诉你们我之前尝试过的东西。这是我的控制台从我编写的System.outs中看到的内容。

01-16 13:32:15.473: I/System.out(1620): We can read/write.
01-16 13:32:15.473: I/System.out(1620): Read File Successfully
01-16 13:32:15.473: I/System.out(1620): null
01-16 13:32:15.473: I/System.out(1620): null
01-16 13:32:18.553: I/System.out(1620): Read Balance File Successfully
01-16 13:32:18.553: I/System.out(1620): 0.00
01-16 13:32:18.553: I/System.out(1620): 0.00
01-16 13:32:18.563: I/System.out(1620): 16/01/14 13:32:18
01-16 13:32:18.563: I/System.out(1620): Longitude: -76.13065846 Latitude: 36.78333427
01-16 13:32:21.233: I/System.out(1620): Read Balance File Successfully
01-16 13:32:21.233: I/System.out(1620): 11.00
01-16 13:32:21.233: I/System.out(1620): 11.00
01-16 13:32:21.243: I/System.out(1620): 16/01/14 13:32:21
01-16 13:32:21.243: I/System.out(1620): Longitude: -76.13065846 Latitude: 36.78333427
01-16 13:32:21.593: D/ViewRootImpl(1620): ViewPostImeInputStage ACTION_DOWN
01-16 13:32:21.693: E/ViewRootImpl(1620): sendUserActionEvent() mView == null
01-16 13:32:22.283: D/ViewRootImpl(1620): ViewPostImeInputStage ACTION_DOWN

正如您所看到的,它应该记录两个实例,但只能读取其中一个实例。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

确保正确写出btw,只需打开生成的文件进行检查。但除此之外,你可以这样做来阅读整个文件:

StringBuilder builder = new StringBuilder();
String str = null;
while((str = reader.readLine()) != null) builder.append(str+"\n");
String endString = builder.toString();

答案 1 :(得分:-1)

您只使用过readLine次功能一次。像这样使用它:

try {
        InputStream inputStream = openFileInput("history");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }

    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }