PrintWriter is a print output stream of character types, inherited from writer, used to print the formatted representation of an object to the text output stream.
Main functions of PrintWriter:
PrintWriter (OutputStream out) PrintWriter (OutputStream out, Boolean AutoFlush) PrintW
A good memory is better than a pen.
Attention:PrintWriter only output write function, but no read functionPrintWriter is the character output, PrintStream is the character outputThe bottom of the PrintWriter is still implemented by BufferedWriter and Bufferedstreamwriter.
Package com.
Ckinghan.iodemo;
Import Java.io.BufferedReader;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
bufferedwriter: Writes text to the character output stream, buffering individual characters to provide efficient writes of individual characters, arrays, and strings. The Write () method allows you to output the acquired character and then wrap it through newline (). The character stream in BufferedWriter must be brushed out by calling the Flush method. and BufferedWriter can only operate on character streams. If you want to operate on a byte stream, use Bufferedinputstream.
PrintWriter Introduction
PrintWriter is a print output stream of character types that inherits from writer.
PrintStream is used to print the formatted representation of an object to the text output stream. It implements all the print methods in the PrintStream. It does not contain methods for writing raw bytes, which should be written with an encoded stream of bytes.
Let's take a look at the source code of JUnit. At the beginning, I found a piece of interesting code:
public String trace() { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); thrownException().printStackTrace(writer); StringBuffer buffer = stringWriter.getBuffer(); return buffer.toString(); }
I have never touched Strin
1 Inherit from Java.io.writer classJspWriter directly with the Out object on the JSP page, or you can get the out object directly with Pagecontex.getout ()PrintWriter must be obtained by Response.getwriter ()2. In the JSP page in two ways to output data at the same time, the data in the PrintWriter will first outputFor example:New Test.jsp PageOut.println ("Out");JspWriter out1 = Pagecontext.getout ();if (o
1 All inherit from Java. Io. Writer class
Jspwriter can directly use the out object output on the JSP page, or use pagecontex. getout () to obtain the out object.
Printwriter must be obtained through response. getwriter ()
2. Use two methods to output data simultaneously on the JSP page. Data in printwriter is output first.
For example:
Create a test. jsp page
Out. println ("out ");Jspwriter out
1. PrintWriter's print and println methods can accept parameters of any type, while the write method of BufferedWriter can only accept characters, array of characters, and strings;
2. PrintWriter's Println method automatically adds line wrapping, BufferedWriter needs to display the calling newline method;
3. The PrintWriter method does not throw the exception, if concerns the exception, needs to call the CheckError method to see whether has the unusua
Today in learning socket communication, write a simple demo, run a problem, the client writes the data and calls the Flush method, the server side did not receive the data. The Client main code is as follows:
Socket socket = new Socket (ipaddress, 2017);
BufferedReader inreader = new BufferedReader (new InputStreamReader (
Socket.getinputstream ()));
PrintWriter writer = new
[] args) throws Exception {Socket client = new Socket("127.0.0.1", 3000);OutputStream os = client.getOutputStream();PrintWriter pw = new PrintWriter(os, true);pw.print("Hello,Server");pw.close();// pw.println("Hello,Server");}}
The code is very simple, mainly because the client sends a string to the server and the server prints the received information.
After the above Code is run (server. Java is run first
1. All are inherited from the java. Io. Writer class.
Jspwriter can directly use the out object output on the JSP page. You can use pagecontext. getout (); to obtain the jspwriter object.Printwrietr must use response. getwriter () in the JSP page to obtain its object. The two have different scopes.
2. Call two methods to output data to the page at the same time on the JSP page. The data in printwriter is output before jspwriter.
For example:
J
PrintStream and PrintWriter APIs are almost identical, and can output all forms of data in almost the same way
PrintWriter to accept writer parameters.
API comparisons:
So, what is the difference between them? From Stackflow
The main meaning is that 2 classes function basically the same, PrintStream can do printwriter also can be achieved, and
Label: Style Color OS ar Java SP strong on
PrintwriterClass is used to replace Java 1.0PrintstreamClass, which can correctly process multi-byte character sets and international text. Sun initially planned to abandon printstream and support printwriter, but gave up the idea when it realized that doing so would invalidate too many current Code (especially code dependent on system. out. However, newly written code should still use
JSP Nine hidden ObjectsJspWriter outJspWriter is an abstract class and PrintWriter is not, which means that you can create a new PrintWriter object directly by using the new operation and JspWriter not, it must be created by its subclass.But they are also related, the relationship is mainly jspwriter to printwriter dependence. When initializing a JspWriter object
Because of the simplicity of printwriter, which requires only print () statements to output anything, there are a lot of times to use this class, but I often overlook the construction method: New PrintWriter (New FileOutputStream (), True);
If you don't add this true, the consequences are incredible ...
Not only in the operation of the file stream, but also in the network programming socket, the output s
The project uses Android and C + + communication, choose the way to send characters with the socket, the first use of the code is:Socket=new Socket (); Inetsocketaddress isa = new Inetsocketaddress (ip_str, port); Socket.connect (ISA, 100); PrintWriter out=new PrintWriter (Socket.getoutputstream (), true); BufferedReader recvbuf=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));In th
Package namespace; import Java. io. *; public class iotest {public static void main (string ARGs []) throws ioexception {bufferedreader bufreader = new bufferedreader (New inputstreamreader (system. in); printwriter PW = new printwriter (New bufferedwriter (New filewriter ("F: \ java.txt"); string line = NULL; while (true) {line = bufreader. readline (); If (line. equals ("over") break; PW. println (line);
1.OutputStream
Use steps: Get output stream set Chinese convert a string to a byte array call Outputstream.write ()
Here only the contents of the Doget method are posted:
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException c2/>//gets OutputStream output stream
outputstream outputstream = Response.getoutputstream ();
Set Chinese
response.setheader ("Content-type", "Text/html;charset=utf-8");
St
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.