作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在那里明确writer.flush()
或不?我想离开后save()
方法 writer
可能无法将数据写入 outputStream
import java.io.*;
public class Test
{
public int i = 5;
public void save(OutputStream outputStream) throws Exception
{
PrintWriter writer = new PrintWriter(outputStream);
writer.println(i);
writer.flush(); // necessarily or not?
}
}
最佳答案
来自 flush
文档:
/**
* Flushes the stream. If the stream has saved any characters from the
* various write() methods in a buffer, write them immediately to their
* intended destination. Then, if that destination is another character or
* byte stream, flush it. Thus one flush() invocation will flush all the
* buffers in a chain of Writers and OutputStreams.
*
* <p> If the intended destination of this stream is an abstraction provided
* by the underlying operating system, for example a file, then flushing the
* stream guarantees only that bytes previously written to the stream are
* passed to the operating system for writing; it does not guarantee that
* they are actually written to a physical device such as a disk drive.
*
* @throws java.io.IOException
* If an I/O error occurs
*/
checkError
根据其文档:
* Flushes the stream if it's not closed and checks its error state.
关于java - 我需要在那里显式使用 flush() 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65780687/
我是一名优秀的程序员,十分优秀!