PrintStream (print output stream) and javaprintstream in Java

Source: Internet
Author: User

PrintStream (print output stream) and javaprintstream in Java
PrintStream in Java (print output stream)PrintStream is a print output stream that inherits from FilterOutputStream. PrintStream is used to decorate other output streams. It can be added to other output streams so that they can easily print various data value representations. Unlike other output streams, PrintStream never throws an IOException. The IOException generated by PrintStream is captured by its own function and an error mark is set. You can return an error mark through checkError, check whether IOException is generated in PrintStream. In addition, PrintStream provides automatic flush and Character Set setting. Automatic flush means that the flush () function is immediately called for data written to PrintStream.




Common ConversionsFileReader --> BufferedReaderBufferedReader in = new BufferedReader (new FileReader ("Text. java "); InputStream --> InputStreamReader --> BufferedReaderBufferedReader in = new BufferedReader (new InputStreamReader (System. in); String --> byte [] --> ByteArrayInputStream --> DataInputStreamDataInputStream in = new DataInputStream (new ByteArrayInputStream (str. getBytes (); FileInputStream --> BufferedInputStream --> DataInputStreamDataInputStream in = new DataInputStream (new BufferedInputStream (new FileInputStream ("Data.txt "))); fileWriter --> BufferedWriter --> PrintWriterPrintWriter pw = new PrintWriter (new BufferedWriter ("text. out "); System. out (PrintStream) --> PrintWriterPrintWriter pw = new PrintWriter (System. out, true); FileOutputStream --> BufferedOutputStream --> PrintStreamPrintStream ps = new PrintStream (new BufferedOutputStream (new FileOutputStream ("text. out "); FileOutputStream --> BufferedOutputStream --> DataOutputStreamDataOutputStream dos = new DataOutputStream (new BufferedOutputStream (new FileOutputStream (" Data.txt "))); Constructor:PrintStream (File file) creates a new print stream with the specified File and without automatic row refresh. PrintStream (File file, String csn) creates a new print stream with the specified File name and character set without automatic row refresh. PrintStream (OutputStream out) creates a new print stream. PrintStream (OutputStream out, boolean autoFlush) creates a new print stream. PrintStream (OutputStream out, boolean autoFlush, String encoding) creates a new print stream. PrintStream (String fileName) creates a new print stream with the specified file name without automatic row refresh. PrintStream (String fileName, String csn) creates a new print stream with the specified file name and character set without automatic row refresh.Similarities and differences between PrintStream and DataOutputStreamSimilarities: they both inherit from FileOutputStream and are used to wrap other output streams. Differences: (01) both PrintStream and DataOutputStream can format and output data, but they are encoded differently when "output string" is used. PrintStream uses the user-specified encoding for output (specified when PrintStream is created). If it is not specified, the default character encoding is used. DataOutputStream uses UTF-8. For more information about UTF-8 character encoding, refer to character encoding (ASCII, Unicode, and UTF-8) and size terminals. For more information about DataOutputStream, for details, refer to the cognition, source code, and example of "DataOutputStream (data output stream) of java I/O SERIES 15" (02). The exception handling mechanism is different when they write data. When DataOutputStream writes data to the "output stream" through write (), it throws an IOException. When PrintStream writes data to the "output stream" through write (), if IOException is generated, it will capture and process the data in write; set the trouble flag (used to indicate exceptions) to true. You can use checkError () to return the trouble value to check whether exceptions occur in the output stream. (03) There is only one constructor for different DataOutputStream constructors: DataOutputStream (OutputStream out ). That is, it only supports the output stream out as the "output stream of DataOutputStream ". PrintStream constructor has many: Like DataOutputStream, it supports output stream out as the constructor of "PrintStream output stream; it also supports constructors of "File object" or "String type File name object. In addition, in the PrintStream constructor, you can specify the character set and whether automatic flush () operations are supported ". (04) Different DataOutputStream functions to decorate other output streams. It works with DataInputStream to allow applications to read and write java data types from the underlying input stream in a machine-independent manner. Although PrintStream is used to describe other output streams, it does not aim to read or write java data types from the underlying layer in a machine-independent manner; instead, it provides various data value representations for other output streams, allowing other output streams to conveniently output data in various formats, such as print (), println (), and printf. Http://www.apihome.cn/api/java/PrintStream.htmlhttp://www.cnblogs.com/skywang12345/p/io_16.html

Related Article

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.