Writing Text


PrintWriter out =    new PrintWriter(socket.getOutputStream(), true); out.print(msg); out.flush();



This phrase assumes that we've previously created a socket to the server from which we want to write text. See the phrase "Contacting a Server" in this chapter for more details on creating the socket instance. Given the socket instance, we call the getOutputStream() method to get a reference to the socket's output stream. With that, we instantiate a PrintWriter for writing text across the network to the server with which we are connected. The second parameter that we pass to the PrintWriter constructor in this phrase sets the auto-flush option. Setting this to true will cause the println(), printf(), and format() methods to automatically flush the output buffer. In our phrase, we use the print() method; thus, we must follow that by a call to the flush() method to force the data across the network.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net