Stream reader and writer)

Source: Internet
Author: User
Tags protected constructor

Writer classIt is never used directly because it is an abstract class (with two protected constructor ). It is used in a multi-state manner through a subclass of it. It hasFive write () MethodsAndFlush ()AndClose ()Method:

Protected writer () // constructor protected writer (Object lock) // constructor public abstract void write (char [] Text, int offset, int length) throws ioexceptionpublic void write (INT c) throws ioexceptionpublic void write (char [] Text) throws ioexceptionpublic void write (string s) throws ioexceptionpublic void write (string S, int offset, int length) throws ioexceptionpublic abstract void flush () throws ioexceptionpublic abstract void close () throws ioexception

The write (char [] Text, int offset, int length) method is the basic method, and the other four write () methods are implemented based on it. Subclass must cover at least this method and the flush () and close () methods. However, to provide more efficient implementation methods, most subclass also covers other write () methods.Note:Different encoding methods may write different byte sequences. The specific output depends on the encoding method,All child classes of the writer class have constructors of the specified character set..

Writer canBufferIt is possible to chain directly to bufferedwriter or indirectly (because the underlying output stream is buffered ). To force a write request to be submitted to the output media, call the flush () method:

w.flush()

The behavior of the close () method is similar to that of the close () method of outputstream. Close () refresh the output writer, close the underlying output stream, and release all associated resources:

public abstract void close() throws IOException

In addition, when the writer is disabled, an ioexception is thrown for further writing.

Let's look at an example of a writer:

Package test; import Java. io. ioexception; import Java. io. outputstreamwriter; public class readfile4 {public static void main (string [] ARGs) {// note that the Try (outputstreamwriter W = new outputstreamwriter (system. out, "UTF-8") {string S1 = "Japanese: Japanese English. write (S1); W. flush ();} catch (ioexception e) {e. printstacktrace ();}}}


Stream reader and writer)

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.