【问题标题】:Writing output data to text file gives incomplete result in text file将输出数据写入文本文件会导致文本文件中的结果不完整
【发布时间】:2013-10-21 22:09:30
【问题描述】:

我有 14 个列表,每个列表都有数字或字符串数​​据。每个列表的大小为 32561。我必须输出格式为:

list1_element1   list2_element1.......................list14_element1
list1_element2   list2_element2.......................list14_element2
.
.
.
.
.
list1_element32561   list2_element32561.......................list14_element32561

输出文件中的每个元素都由制表符分隔。我在 Java 中是这样做的。

        out = new BufferedWriter(new FileWriter(fileName));
        for(int i=0; i<32561; i++) {
        out.write(firstList.get(i));
        out.write("\t");
        out.write(secondList.get(i));
        out.write("\t");
        out.write(thirdList.get(i));
        out.write("\t");
        out.write(fourthList.get(i));
        out.write("\t");
        out.write(fifthList.get(i));
        out.write("\t");
        out.write(sixthList.get(i));
        out.write("\t");
        out.write(seventhList.get(i));
        out.write("\t");
        out.write(eighthList.get(i));
        out.write("\t");
        out.write(ninthList.get(i));
        out.write("\t");
        out.write(tenthList.get(i));
        out.write("\t");
        out.write(eleventhList.get(i));
        out.write("\t");
        out.write(twelvethList.get(i));
        out.write("\t");
        out.write(thirteenthList.get(i));
        out.write("\t");
        out.write(fourteenthList.get(i));
            out.write("\t");
        out.write(fifteenthList.get(i));

        out.newLine();
    }
}

我的程序仅打印 32441 行,最后一行仅包含 6 列。我不明白为什么。有人可以帮忙吗?

【问题讨论】:

    标签: java bufferedreader filewriter


    【解决方案1】:

    您没有关闭Writer,因此您正在丢失缓冲区中的任何内容。

    【讨论】:

      【解决方案2】:

      缓冲区只有在满时才会写入流,或者您在其上调用 writer.flush() 或 writer.close() 函数。在您的情况下,您只需要添加

      out.close()
      

      在程序结束时。否则,您的缓冲区中的数据不会在程序结束时写入,而不会告诉作者写入其缓冲区的剩余内容;假设会有更多数据,它会坚持下去!

      【讨论】:

        【解决方案3】:

        您尚未提供完整的方法代码,但问题可能是“未关闭写入器且未正确刷新输出缓冲区”。

        当您需要刷新输出缓冲区时使用out.flush();。当您结束写入文件时,out.close;

        或者

        您可以使用 PrintWriter:

        PrintWriter writer = new PrintWriter(new FileWriter("file name"), true);
        

        在 writer 调用 println() 或 format() 或 printf() 时自动刷新输出缓冲区。或者您可以在需要时手动使用 writer.flush() 刷新输出缓冲区。

        【讨论】:

        • 关闭前不必冲洗。
        【解决方案4】:

        你可以使用 BufferedWriter:

            try (OutputStream outputStream = new FileOutputStream("applicationInfo.json")){
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
                var gson = new GsonBuilder()
                        .serializeNulls()
                        .create();
                gson.toJson(applicationInfo, JsonArray.class, bufferedWriter);
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-01
          • 1970-01-01
          相关资源
          最近更新 更多