The function and usage of flush of in Java language are explained in detail

  • 2020-12-19 21:01:39
  • OfStack

Recently, when studying io flow, I found that the flush() function appeared every time. I checked 1 of its functions, and the main functions are as follows

The function of flush() is to

General and wrong answer:

The data in the buffer is not written out until the buffer is full, or the flush method can be used to force the data in the buffer to be written out or the close() method can be used to close the stream. The buffer output stream writes out the buffer data once before closing the stream. Both flash() and close() force the data to be written out, so the two results are one, and if neither is written, you will find that it cannot be written out successfully

In response to the above answers, give an accurate answer

FileOutPutStream inheritance outputStream, does not provide flush rewriting () method so that no matter how many write content will pass 2 base flow directly to the underlying operating system I/O flush Buffered series of input and output stream function without effect can be seen from the word Buffered alone they are using buffer, and equipment application every IO to communicate, efficiency is very low, so the buffer in order to improve the efficiency, when written to the device, first written to the buffer, wait until the buffer when we have enough data Write to the device as a whole

Use BufferedXXXStream. The default buffer size is 8K. It fills up the buffer when it reads (or when the file is read), and it doesn't enter the kernel buffer until it fills up when it writes (or performs the flush operation). The reason for efficiency is to avoid getting stuck in the operating system kernel for every byte read (which is a time-consuming operation). For the specific code, let's look up API.

Attached again from Baidu know 1 paragraph of question answer.

flush() means to force the contents of the buffer to be written. Because some mechanism of the operating system, in order to prevent 1 straight kept on disk read and write, so has the concept of delay to write, (be careful not to and frush () to refresh the confused), mainly used in IO, namely empty buffer data, 1 at the time of reading and writing flow (stream), data is to be read into memory first, then the data written to a file, when you read the data do not represent your data has been finished, because there is 1 part is likely to remain in the memory in the buffer. If you call close() to close the read/write stream, this data will be lost, so flush() should be used before closing the read/write stream.

On network web servers, too, there is the concept of a buffer, such as the memory area of 64K, that is written to the disk for a second write (or sent to the client browser), in order to prevent sending one message per byte.

flush method 1 is typically executed when the program has finished writing. This is followed by the close method. Such as:


//  Gets the output stream. Of course, it depends on the context. 
PrintWriter out = Util.getWriter();
out.println(" The output 1 Some information, maybe a lot ");
out.flush();
out.close();

Simply put, the flush () method outputs everything stored in memory (batch output). A typical example is the FileWriter class, where the flush output can be used, and the close method can be called at the end for batch output.


import java.io.FileWriter;
import java.io.IOException;
public class A04 {
	public static void main(String args[]) {
		FileWriter fw;
		try {
			fw = new FileWriter("C:\\try.txt");
			fw.write("Hello");
			//fw.close(); // This sentence does not add, cannot write the file, the file is blank!  System.out.println("OK"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

conclusion

Above is the article on Java language flush function function and use methods to explain the full content, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: