Introduction to the usage of flush in JAVA IO stream

Source: Internet
Author: User
Tags flush serialization socket

Objective:

We are using Java io stream outputstream, printwriter ... , the Flush () method is often used.

A Why to Flush:

Like caching in network hardware, streams can also be cached in software, which is cached directly in Java code. This can be achieved by Bufferedoutputstream or bufferedwriter links to the underlying stream. Therefore, in writing

When the data is finished, flush is particularly important.

For example:


The Web server in the above image responds to a 300-byte message by outputting to the client, but the output stream has a 1024-byte buffer. So, the output stream waits until the Web server continues to respond to the client, and when the Web server's response information fills the buffer in the output stream, the output stream responds to the Web client message.

In order to solve this embarrassing situation, the flush () method appears. The flush () method can force the output stream (or buffered stream) to send data, even if the buffer is not filled at this time, to break the deadlock state.

When we send data using an output stream, when the data cannot fill the buffer of the output stream, the data is stored in the buffer of the output stream. If, at this time, we call the shutdown (close) output stream, the data stored in the buffer of the output stream is lost. So, when you close the output stream, you should refresh (flush) The output stream, in other words: "Force all buffered output data to be written to the underlying output stream".

Two Interpretation of Flush () Source:

Here's an example of the Bufferedoutputstream class:

Publicclass Bufferedoutputstream extends Filteroutputstream
{
Publicsynchronizedvoid Flush () throws IOException
{
Flushbuffer ();
Out.flush ();
}

Privatevoid Flushbuffer () throws IOException

{
if (Count > 0)
{
Out.write (buf, 0, Count);
Count = 0;
}
}
}
See here everyone understand it, in fact flush () is also through the Out.write () to write data to the underlying output stream

Java IO streaming face questions and multiple-choice questions

1.PrintStream, BufferedWriter, printwriter comparison?
The output of the PrintStream class is very powerful, usually if you need to output text content, you should wrap the output stream into PrintStream after the output. It also provides two other features. Unlike other output streams, PrintStream never throws IOException; instead, exceptions only set internal flags that can be tested by the CheckError method. In addition, to automatically refresh, you can create a PrintStream
BufferedWriter: Writes text to the character output stream, buffering individual characters to provide efficient writes of individual characters, arrays, and strings. The Write () method allows you to output the acquired character and then wrap it through newline (). The character stream in BufferedWriter must be brushed out by calling the Flush method. and BufferedWriter can only operate on character streams. If you want to operate on a byte stream, use Bufferedinputstream.
PrintWriter's Println method automatically adds line wrapping, does not throw the exception, if concerns the exception, needs to call the CheckError method to see whether has the unusual occurrence, PrintWriter constructs the method may specify the parameter, realizes automatically flushes the cache (AutoFlush);

There are several types of streams in 2.java? The JDK provides a number of abstract classes for each type of stream for inheritance, what are the classes they are, respectively?
BYTE input stream: InputStream, byte output stream: OutputStream
Character input stream: Reader, character output stream: Writer

3. What is Java serialization and how do I implement Java serialization?
Serialization of Java objects refers to writing a Java object into the Oi stream, which corresponds to the deserialization of the object that restores the Java object from the IO stream.
If you want an object to support serialization mechanisms, you must make its class serializable so that a class is serializable, it must implement either the serializable interface or the Externalizable interface


4. Explain the Java.io.Serializable interface (interview often test)
Class enables its serialization functionality by implementing the Java.io.Serializable interface. A class that does not implement this interface will not be able to serialize or deserialize any of its states.
1. Read and write raw data, general use what flow? (AC)
A InputStream
B DataInputStream
C OutputStream
D Bufferedinputstream
2. What stream can be used to improve read and write performance? (DF)
A InputStream
B DataInputStream
C BufferedReader
D Bufferedinputstream
E OutputStream
F Bufferedoutputstream
3. What stream is used for various basic data types and string types of reading and writing? (AD)
A DataInputStream
B BufferedReader
C PrintWriter
D DataOutputStream
E ObjectInputStream
F ObjectOutputStream
4. The type of I/O stream that can specify character encoding is: (BH)
A Reader
B InputStreamReader
C BufferedReader
D Writer
E PrintWriter
F ObjectInputStream
G ObjectOutputStream
H OutputStreamWriter
5. What method is defined in the file type to determine whether a file exists? D
A CreateNewFile
B Renameto
C Delete
D exists
6. What methods are defined in the file type to create a level of directory? C
A CreateNewFile
B exists
C mkdirs
D mkdir
7. What I/O stream is used for text file operations? (AD)
A FileReader
B FileInputStream
C Randomaccessfile
D FileWriter
8. A TCP-based time service application is available on UNIX server www.openlab.com.cn, which uses port 13. Create a statement to connect to this server: (A)
A socket s = new socket ("www.openlab.com.cn", 13);
B socket s = new socket ("www.openlab.com.cn:13");
C Socket s = Accept ("www.openlab.com.cn", 13);
9. Create a TCP client program in the order that: (DACBE)
A get I/O stream
B Close I/O stream
C reads and writes to I/O stream
D Establish socket
E Close Socket
10. Create a TCP service program in the order that: (BCADEGF)
A creates a service thread to handle the new connection
B Create a server socket
C Accept client connection request from server socket
D in the service thread, get the I/O stream from the socket
E read/write to the I/O stream to complete the interaction with the customer
F Close Socket
G Close I/O stream
One of the two main types of Java UDP programming are: (BD)
A Udpsocket
B Datagramsocket
C Udppacket
D Datagrampacket
TCP/IP is one of the following: (B)
A Standard
B protocol
C language
D algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.