The java.io.Writer.flush() method flushs 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.
public class Demo {public static void main(String[] ars) throws Exception {System.out.println("hello");PrintWriter writer = new PrintWriter(System.out);writer.println("writer start");// writer.flush();try {Thread.sleep(3000);}catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}writer.println("writer close");writer.close();}}As in the above code, if flush() is commented out, "writer start" and "writer close" will be printed 3 seconds after printing "hello", because writer.close() will call flush() once before closing the output stream. The effects are as follows:
If flush() is not commented out, then "writer start" will be printed immediately after printing "hello".
Summarize
The above is the entire content of this article about the code example of the flush() function in IO, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!