[I/O] character streams

Source: Internet
Author: User

Directory:

Character streams

Reader

Read ()

Read (char [] Buf, int offset, int count)

Read (char [] BUF)

Read (Java. NiO. charbuffer BUF)

Skip (Long Count)

Ready ()

Close ()

Writer

Write (INT ch)

Write (char [] Buf, int offset, int count)

Write (char [] BUF)

Write (string STR, int offset, int count)

Write (string Str)

Flush ()

Close ()

Character streams: character is a 16-bit UTF-16 character, text-based I/O Stream is a readable character, such as the source code of the program. charater streams is called readers and writers. most input or output streams have corresponding reader or writer streams, and vice versa. like byte streams, character streams should also be explicitly closed to release resources.

Character streams type tree in Java. Io package:

Reader

Public intRead ()Throws ioexception

Purpose: read a single character.

Description: The value range of the returned value is 0-65535. The read data is 16 characters lower than the int type data. If it reaches the end of the stream and has no characters to read,-1 is returned.

Eg.

package mjn.io;import java.io.FileReader;import java.io.IOException;import java.io.Reader;/** * count the total characters  * and spaces of a text file *  * @author MJN * @date   2011-09-30 */public class CountSpaces {    public static void main(String[] args) throws IOException {        Reader in = new FileReader("in.txt");        int total = 0;        int spaces = 0;        int ch;        while ((ch = in.read()) != -1) {            if (Character.isWhitespace((char) ch)) {                spaces++;            }            total++;        }        System.out.println(total + " chars, " + spaces + " spaces.");    }}

Contents of the in.txt file:

hello world!

Program output:

12 chars, 1 spaces.

Public intRead (char [] Buf, int offset, int count)Throws ioexception

Purpose: read count characters to the array Buf (from Buf [offset] To Buf [Offset + count-1]), and return the actual number of characters read.

Note: If the stream is closed for other reasons (not at the end of the stream), an ioexception is thrown.

Public intRead (char [] BUF)Throws ioexception

Purpose: Run read (BUF, 0, Buf. length ).

Public intRead (Java. NiO. charbuffer BUF)Throws ioexception

Purpose: read as many characters as possible into the Buf without Overflow.

Public longSkip (Long Count)Throws ioexception

Purpose: Skip count characters

Public BooleanReady ()Throws ioexception

Purpose: whether the stream is ready.

Note: at least one character can be read.

Public abstract voidClose ()Throws ioexception

Purpose: Disable the character input stream.

Writer

Public voidWrite (INT chThrows ioexception

Purpose: write a single character into a stream

Note: Only the lower 16 bits of ch are written.

Public voidWrite (char [] Buf, int offset, int count)Throws ioexception

Purpose: Write the Count characters of the character array Buf into the stream (from Buf [offset] To Buf [Offset + count-1]).

NOTE: If count> Buf. Length-offset, a java. Lang. indexoutofboundsexception exception is thrown.

Public voidWrite (char [] BUF)Throws ioexception

Purpose: Execute write (BUF, 0, Buf. length ).

Public voidWrite (string STR, int offset, int count)Throws
Ioexception

Purpose: Write count characters of the string STR into the stream (from Str. charat (offset) to str. charat (Offset + count-1 )).

Public voidWrite (string Str)Throws ioexception

Purpose: Execute write (STR, 0, str. Length ()).

Public abstract voidFlush ()Throws ioexception

Purpose: refresh the buffer.

Public abstract voidClose ()Throws ioexception

Purpose: Close the stream and release resources.

References:

[1] The Java programming language, Fourth Edition

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.