在 java.io.FileWriter 中刷新 [英] flush in java.io.FileWriter

查看:25
本文介绍了在 java.io.FileWriter 中刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,在写入文件时,在关闭完成之前,我们是否应该包含flush()??.如果是这样,它会做什么?不流自动冲洗?

I have a question in my mind that, while writing into the file, before closing is done, should we include flush()??. If so what it will do exactly? dont streams auto flush??

那么它实际上做了什么?

So flush what it actually do?

推荐答案

写入器和流通常会在内存中缓冲一些输出数据,并尝试一次将其写入更大的块中.刷新将导致立即从缓冲区写入磁盘,因此如果程序崩溃,数据不会丢失.当然不能保证,因为磁盘可能不会立即物理写入数据,因此它仍然可能丢失.但这不是 Java 程序的错:)

Writers and streams usually buffer some of your output data in memory and try to write it in bigger blocks at a time. flushing will cause an immediate write to disk from the buffer, so if the program crashes that data won't be lost. Of course there's no guarantee, as the disk may not physically write the data immediately, so it could still be lost. But then it wouldn't be the Java program's fault :)

PrintWriters 在您编写行尾时自动刷新(默认情况下),当然流和缓冲区在您关闭时刷新.除此之外,只有当缓冲区已满时才会刷新.

PrintWriters auto-flush (by default) when you write an end-of-line, and of course streams and buffers flush when you close them. Other than that, there's flushing only when the buffer is full.

这篇关于在 java.io.FileWriter 中刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆