java - I could not understand this that why to use flush() method of a PrintWriter class? -
this question has answer here:
- how use flush() printwriter 2 answers
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
Post a Comment