In the entire java.io package, the print stream is the most convenient class for outputting information, consisting primarily of the byte print stream (PrintStream) and the character print stream (PrintWriter).
PrintStream and PrintWriter are both processing streams.
PrintStream
public class PrintStream
extends Filteroutputstream
implements Appendable, closeable
PrintWriter
public class PrintWriter
extends Writer
printstream v.s. PrintWriter
All two classes implement a similar printxxx method, and for the Writexxx method, PrintStream contains only the output byte method, PrintWriter the method containing only the output characters;
For PrintWriter, when you do not use the incoming writer constructor, PrintWriter constructs a OutputStreamWriter object with the parameters passed in. And then the new one BufferedWriter object out to wrap the OutputStreamWriter object, after which the printxxx operation is done by the bufferedwriter out, and if you use the constructor that passed in the writer, The actual use is the incoming writer object out, whether the buffer is all in the incoming writer object;
In the implementation of PrintStream, a byte character is converted to a Stream object OutputStreamWriter, which converts the byte stream printstream to a character flow. Then a new BufferedWriter object wraps the writer, and viewing the PRINTXXX implementation can find that the Printxxx method is actually the final output of the bufferedwriter.
There is no difference between the two situations where the Printxxx series method is used .
Automatic flush: For PrintStream, if AutoFlush = True is set, the contents of the buffer are forced to output whenever a line break is encountered (calling the Println method, outputting a newline character, or a newline byte ' \ n '), which is automatically called the Flush () function. For PrintWriter, if AutoFlush = True is set, unlike PrintStream, only when println is invoked, printf, the format method automatically flush the contents of the buffer instead of outputting it as if it were encountering a newline character.
Use examples:
Package com.gof.io.test;
Import Java.io.ByteArrayInputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintStream;
Import Java.io.PrintWriter;
public class Printoutputapp {public static void main (string[] args) {InputStreamReader ISR = null;
PrintStream PS = null;
PrintWriter pw = null;
try{//GB2312 Encoding byte[] Barray = {(byte) 0xca, (byte) 0xe4,//Output (Byte) 0xc8, (byte) 0xeb,//Into
(byte) 0xca, (byte) 0xe4,//Output (Byte) 0xb3, (byte) 0xf6//out};
ISR = new InputStreamReader (new Bytearrayinputstream (Barray), "GB2312");
PS = new PrintStream (System.out, true);
PW = new PrintWriter (new OutputStreamWriter (System.out), true);
int data =-1;
while (data = Isr.read ())!=-1) {Ps.print ((char) data);
Pw.print ((char) data);
} ps.flush ();
Pw.flush ();
}catch (Exception e) {e.printstacktrace ();
}finally {try{pw.close ();
Ps.close (); Isr. Close ();
}catch (Exception e) {e.printstacktrace ();
}
}
}
}
Output results:
Input and output input and output