Java021.io Stream Character streams

Source: Internet
Author: User

Attention:
A byte stream can manipulate arbitrary data, but the character stream only works with plain text files, and is generally used for read-only or write-only text files!

What is a character stream?
* Character Stream is an IO stream that can read and write characters directly
* Character Stream reads characters, first reading to byte data, then to characters. If you want to write characters, you need to convert the characters to bytes and write them out.

The read () method of the FileReader class can be read by character size
The write () method of the FileWriter class can automatically convert characters to bytes written out

Character stream usefulness and features:
* Character stream can also copy text files, but not recommended. Because the byte is converted to a character when it is read, the character is returned to the byte when it is written.
* The program needs to read a piece of text, or need to write a piece of text when you can use a character stream
* Read by the size of the characters are read, will not appear half Chinese
* You can write the string directly, without converting it to a byte array.

Character stream can not copy non-plain text files, because in the reading of the byte will be converted to characters, in the conversion process, may not find the corresponding character, it will be used instead, write the character will be converted to byte write out, if it is, write directly, so that the file after writing is messy, can't see

* the Read () method of the BufferedReader reads the characters one time to the buffer and then returns to the program, reducing the number of times the file is read and improving efficiency
* The Write () method of BufferedWriter writes the characters to the buffer, writes to the buffer when it is full, reduces the number of writes, and improves efficiency.

* BufferedReader's ReadLine () method can read one line of characters (without line breaks)//unique method
* BufferedWriter's newline () can output a cross-platform line break symbol "\ r \ n"//Unique method

LineNumberReader is a subclass of BufferedReader, has the same function, and can count line numbers
* Call the Getlinenumber () method to get the current line number
* Call the Setlinenumber () method to set the current line number

* FileReader is to use the default Code table to read files, if you need to use the specified code table read, then you can use InputStreamReader (byte stream, encoding table)
* FileWriter is using the default Code table to write out the file, if you need to use the specified code table written out, then you can use OutputStreamWriter (byte stream, encoding table)

Practice:
* 1. The characters of the GBK and UTF-8 codes will be read with BufferedReader
* 2. Use BufferedWriter to write characters to the GBK Code table and UTF-8 Code table files
* 3. Use BufferedReader to read a line from the keyboard
* 4. Receive a folder path from the keyboard input and print out all the. java file names under that folder

Java021.io Stream Character streams

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.