Write () and println () differences in JAVA PrintWriter

Source: Internet
Author: User

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 ()

    • In addition, I just summed up the key program section of my problem, don't say I did not refresh the server socket output stream.

      It's better for someone to explain the writer in the output stream in IO. And how to extract the characters that are written to the stream with the write () method from an output stream.

    • 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

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.