Java.io.Writer.flush() 方法
描述
java.io.Writer.flush() 方法刷新流。 如果流已将来自各种 write() 方法的任何字符保存在缓冲区中,请立即将它们写入其预期目的地。 然后,如果该目的地是另一个字符或字节流,则刷新它。 因此,一次 flush() 调用将刷新 Writers 和 OutputStreams 链中的所有缓冲区。
声明
以下是 java.io.Writer.flush() 方法的声明。
public abstract void flush()
参数
NA
返回值
此方法不返回值。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.Writer.flush() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class WriterDemo {
public static void main(String[] args) {
String s = "Hello World";
// create a new writer
Writer writer = new PrintWriter(System.out);
try {
// append a string
writer.append(s);
// flush the writer
writer.flush();
// append a new string in a new line
writer.append("\nThis is an example");
// flush the stream again
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Hello World This is an example

