java - I could not understand this that why to use flush() method of a PrintWriter class? -


this question has answer here:

why code here using flush() method of printwriter class after getting message messagebox field....?

public void connecttosocket() {      try {         socket socket = new socket("localhost", 80);         printwriter writer = new printwriter(socket.getoutputstream);         system.out.println("connected successfully");     }     catch(ioexception ex) {             ex.printstacktrace();     } }  public class sendmessagelistener implements actionlistener {         try {              writer.println(messagebox.gettext());          writer.flush();     }     catch(ioexception ex) {              ex.printstacktrace();     } } 

the default constructor printwriter not autoflushes contents. means uses internal buffer since writing operations expensive.

if want write buffer stream, call flush method.

if had used constructor autoflush

printwriter writer = new printwriter(socket.getoutputstream,true); 

you not need call flush method, because flush internal buffer new line command (println() in case) called.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -