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

查看:170
本文介绍了在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天全站免登陆