FileWriter – 如果没有使用 try-with-resource,数据不会完全写入文件

huangapple go评论77阅读模式
英文:

FileWriter - without try-with-resource data is not fully written to file

问题

以下是您要翻译的内容:

我想向文本文件中写入100万行。

当我使用FileWriter而没有使用try-with-resource风格(未释放资源)时,我发现它在大约998976左右停止。

...
998968
998969
998970
998971
998972
998973
998974
998975
998976
9
@Test
void writeTooLargeFileThisIsBad() throws IOException {
    File newFile = new File("src/test/resources/targets/large.csv");
    if (!newFile.exists()) newFile.createNewFile();
    FileWriter writer = new FileWriter("src/test/resources/targets/large.csv", StandardCharsets.UTF_8);
    for (int i = 1; i < 1000000; i++) {
        writer.write(String.valueOf(i));
        writer.write(System.lineSeparator());
    }
}

但是当我使用try-with-resource时,它正常完成。(达到999999)

两者都非常快。

为什么呢?

@Test
void writeTooLargeFileThisIsGood() throws IOException {
    File newFile = new File("src/test/resources/targets/large.csv");
    if (!newFile.exists()) newFile.createNewFile();
    try (FileWriter writer = new FileWriter("src/test/resources/targets/large.csv", StandardCharsets.UTF_8)) {

        for (int i = 1; i < 1000000; i++) {
            writer.write(String.valueOf(i));
            writer.write(System.lineSeparator());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
英文:

I want to write 1 million lines to a text file.

When I use FileWriter without try-with-resource style(not releasing resources), I found it stops at around 998976 and so.

...
998968
998969
998970
998971
998972
998973
998974
998975
998976
9
    @Test
    void writeTooLargeFileThisIsBad() throws IOException {
        File newFile = new File(&quot;src/test/resources/targets/large.csv&quot;);
        if (!newFile.exists()) newFile.createNewFile();
        FileWriter writer = new FileWriter(&quot;src/test/resources/targets/large.csv&quot;, StandardCharsets.UTF_8);
        for (int i = 1; i &lt; 1000000; i++) {
            writer.write(String.valueOf(i));
            writer.write(System.lineSeparator());
        }
    }

But when I do with try with resource, it completes normally. (It reaches 999999)

Both very quick.

Why?

    @Test
    void writeTooLargeFileThisIsGood() throws IOException {
        File newFile = new File(&quot;src/test/resources/targets/large.csv&quot;);
        if (!newFile.exists()) newFile.createNewFile();
        try (FileWriter writer = new FileWriter(&quot;src/test/resources/targets/large.csv&quot;, StandardCharsets.UTF_8)) {

            for (int i = 1; i &lt; 1000000; i++) {
                writer.write(String.valueOf(i));
                writer.write(System.lineSeparator());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

答案1

得分: 3

因为您没有在writer实例上调用close()方法 - 该写入器有一些缓冲区定义,能够存储相当大量的未写入数据。

当您使用try-with-resource方法时,close()会被自动调用,因此每个数据都会被正确地刷新。


在这里阅读更多:

英文:

That's because you are not calling close() method on the writer instance - the writer has some buffer defined able to store quite big amount of unwritten data

When you are using try-with-resource approach the close() is being called automatically so every data is being flushed properly


Read more here:

huangapple
  • 本文由 发表于 2020年9月10日 18:38:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63827908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定