"Java.io" I/O byte/character "process Flow"-"formatted output print stream"-PrintStream, Printwriter__java

Source: Internet
Author: User
Tags flush

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





Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.