What is the subtle difference between the write () method of PrintWriter and the println () method?
Recently learned Java network programming, after the server side and the client generated a socket,
Each side uses the Getiputstream () and Getoutputstream () methods to construct their respective input and output streams.
Where the server-side socket SS
BufferedReader is=new BufferedReader (New InputStreamReader (Ss.getinputstream ()));
PrintWriter os=new PrintWriter (New OutputStreamWriter (Ss.getoutputstream ()));
Os.writer ("A messy string");
Socket CS for Client
BufferedReader is=new BufferedReader (New InputStreamReader (Cs.getinputstream ()));
PrintWriter os=new PrintWriter (New OutputStreamWriter (Cs.getoutputstream ()));
System.out.println (Is.readline ());
Supposedly, the SS OS corresponds to CS's is. Why is it written on the server side and cannot be read on the client side?
In addition, if the server side uses Os.println ("string");
May I ask what this is?
Here's an answer to the open source Chinese community:
-
The print () method is equivalent to the Write () method
The println () method calls a NewLine () method on the basis of print () (Private method)
The NewLine () method calls Flush ()
So the key is flush ()
- If you use PrintWriter, you really should call the println () method instead of the Write method that the parent class integrates. Because the printwriter itself is used to perform the print operation. It is obvious that the write method inherits the method of the parent class directly, and there may be no rewriting or other actions in the subclass.
BTW socket communication is best used DataInputStream and DataOutputStream to package read-write operations, or with ObjectInputStream and ObjectOutputStream, Instead of using the BufferedReader and bufferedwriter you wrote.
- The key is that the client is is.readline (), println is automatically followed by the character carriage return line break, or you os.writer on the server (" a messy string \ r \ n");
- Do you really want to add/r/n for more detailed reasons?
- Check, there are a lot of people on the Internet to ask this question, foreigners a bit more.
The problem is mainly focused on why the server side still does not read the data after the client calls write () and flush (). Because the implementation class that Socket.getoutputstream () takes is Java.net.SocketOutputStream, this class does not overwrite the Flush method, Its parent class Java.io.FileOutputStream also did not overwrite, so eventually transferred to the topmost java.io.OutputStream flush () method, this is disgusting, this method is empty. So...
Therefore, use ReadeRReadLine () and writer.println () to handle the socket, or use NIO directly.
(Note that the Socketoutputstream class does not exist in the Javadoc, only in the source code, he is a protected class.) )
- Light said to me, LZ with the printwriter, I realized the flush, but also called the inner wrapper sub-object, and is controlled by the AutoFlush property, may not be the same as my situation. But can be sure is flush and \ r \ n Problems, you can debug the code to see.
-
The print () method is equivalent to the Write () method
The println () method calls a NewLine () method on the basis of print () (Private method)
The NewLine () method calls Flush ()
So the key is flush ()
- The key is that the client is is.readline (), println is automatically followed by the character carriage return line break, or you os.writer on the server (" a messy string \ r \ n"); This depends on the system. My machine doesn't seem to support it. If you must use write (), just add a sentence printwriter.println (). Because the newline () method is private, the external cannot be called directly
Write () and println () differences in JAVA PrintWriter