Java Io:reader and writer

Source: Internet
Author: User

Author: Jakob Jenkov Translator: Li Jing ([email protected])

Reader

Original link

Reader is the base class for all reader in Java IO. Reader is similar to InputStream, except that reader is based on character rather than byte-based. In other words, reader is used to read text, while InputStream is used to read raw bytes.

Keep in mind that Java internally uses UTF8 encoding to represent strings. One byte in the input stream may not be the same as a UTF8 character. If you read UTF8 encoded text in bytes from the input stream, and you try to convert the bytes read into characters, you may not get the expected results.

The Read () method returns a variable of type int that contains the contents of the read character (translator note: 0~65535). If the method returns-1, indicating that there are no remaining readable characters in reader, you can turn reader off. 1 is an int type, not a byte or char type, which is not the same.

You will typically use the subclass of reader instead of using reader directly. The subclass of reader includes Inputstreamreader,chararrayreader,filereader and so on. You can view the Java IO Overview to browse through the complete reader form.

Reader is typically associated with data sources such as files, character arrays, networks, and so on in the Java IO overview.

Writer

Original link

Writer is the base class for all writer in Java IO. Similar to the relationship between reader and InputStream, writer writes text based on characters rather than bytes, and OutputStream is used to write bytes.

Again, you'd better use writer's subclass, without having to use writer directly, because the subclasses are more explicit in their implementation and more expressive of your intentions. Common subclasses include Outputstreamwriter,chararraywriter,filewriter and so on.

Writer's write (int c) method writes the low 16 bits of the passed-in parameter to writer, ignoring the high 16-bit data.

original articles, reproduced please specify: reproduced from the Concurrent programming network –ifeve.com This article link address: Java Io:reader and writer

Java Io: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.