英文:
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("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());
        }
    }
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("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();
        }
    }
答案1
得分: 3
因为您没有在writer实例上调用close()方法 - 该写入器有一些缓冲区定义,能够存储相当大量的未写入数据。
当您使用try-with-resource方法时,close()会被自动调用,因此每个数据都会被正确地刷新。
在这里阅读更多:
- https://stackoverflow.com/questions/13426142/bufferedwriter-not-writing-everything-to-its-output-file/13426160
 - https://stackoverflow.com/questions/32324492-is-flush-call-necessary-when-using-try-with-resources
 
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论