flush() method is available in java.io package.
flush()方法在java.io包中可用。
flush() method is used to flushes the characters from a write buffer to the character or byte stream.
flush()方法用于将字符从写缓冲区刷新到字符或字节流。
flush() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
flush()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
flush() method may throw an exception at the time of flushing the stream.
flush()方法在刷新流时可能会引发异常。
IOException: This exception may throw an exception while performing input/output operation.
IOException :在执行输入/输出操作时,此异常可能会引发异常。
Syntax:
句法:
public Writer flush();Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is Writer, it returns nothing.
该方法的返回类型为Writer ,不返回任何内容。
Example:
例:
// Java program to demonstrate the example // of Writer flush() method of// BufferedWriterimport java.io.*;public class FlushBW { public static void main(String[] args) { String str = "Java Programming"; try { // Instantiates StringWriter StringWriter str_w = new StringWriter(); // Instantiates BufferedWriter BufferedWriter buff_w = new BufferedWriter(str_w); for (char ch: str.toCharArray()) { // To append character by using // append() to the writer buff_w.append(ch); // It flushes the characters // from buff_w to char or byte // stream buff_w.flush(); // Display Buffer from str_w System.out.println("str_w.getBuffer():" + str_w.getBuffer()); } // Close the stream buff_w.close(); } catch (IOException ex) { System.out.println("buff_w: " + ex.getMessage()); } }}Output
输出量
str_w.getBuffer():Jstr_w.getBuffer():Jastr_w.getBuffer():Javstr_w.getBuffer():Javastr_w.getBuffer():Java str_w.getBuffer():Java Pstr_w.getBuffer():Java Prstr_w.getBuffer():Java Prostr_w.getBuffer():Java Progstr_w.getBuffer():Java Progrstr_w.getBuffer():Java Prograstr_w.getBuffer():Java Programstr_w.getBuffer():Java Programmstr_w.getBuffer():Java Programmistr_w.getBuffer():Java Programminstr_w.getBuffer():Java Programming翻译自: https://www.includehelp.com/java/bufferedwriter-flush-method-with-example.aspx