Java IO stream-character stream

Source: Internet
Author: User

2017-11-05 18:34:39

IO streams in Java are categorized into two types, one is byte stream and the other is character stream. The character stream appears to simplify the read and write operations of text data.

If the file you are manipulating is a text file, using a character stream can greatly simplify the operation, but if you do not know anything, use a byte stream.

    • Character Stream

Because it is not convenient to read Chinese in a byte stream, the conversion stream is provided in Java, so

Character stream = byte stream + encoding table (by the the, ' A ' =97, ' a ' = 65, ' 0 ' =48).

The character stream also has two abstract base classes:Writer

            Reader

* OutputStreamWriter

OutputStreamWriter is a bridge of character flow to a byte stream: it can be charset encoded into bytes using the specified characters that will be written to the stream. The character set it uses can be specified by name or explicitly given, otherwise the platform default character set will be accepted.

Each call to the write () method causes the encoding converter to be called on the given character (or character set). The resulting bytes are accumulated in the buffer before writing to the underlying output stream. You can specify the size of this buffer, but the default buffer is large enough for most purposes. Note that the characters passed to the write () method are not buffered.

For maximum efficiency, consider wrapping the outputstreamwriter into BufferedWriter to avoid frequent calls to the converter.

* * Construction method

* * Common methods

The length of the lenth is written without specifying an offset when writing to a string.

Import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.outputstreamwriter;public class Demo1 {  Public    static void Main (string[] args) throws IOException {        OutputStreamWriter fout = new OutputStreamWriter (new FileOutputStream ("" +                "E:\\text.txt", "GBK"));        Fout.write ("Hello, Java");
Fout.flush (); Fout.close (); }}

Note that the default encoding here is GBK. as stated above, before the final output, the buffer is encoded, so you need to call flush to clear the buffer and output the file. But close () is automatically refreshed before it.

* Inputsteamreader

InputStreamReader is a bridge of byte flow to a character stream: It uses the specified charset read byte and decodes it to a character. The character set it uses can be specified or explicitly given by name, or it can accept the default character set of the platform.

Each call to a read () method in InputStreamReader causes one or more bytes to be read from the underlying input stream. To enable a valid conversion from byte to character, you can read more bytes in advance from the underlying stream to exceed the bytes required to satisfy the current read operation.

In order to achieve maximum efficiency, it is necessary to consider packaging InputStreamReader within the BufferedReader.

* * Construction method

* * Common methods

To simplify the initialization of outputstreamwriter and InputStreamReader, these two classes also provide two subclasses as convenience classes. That

FileWriter class , initialization method FileWriter (File file,bool append), note that the FileWriter class simplifies the build, but cannot set the encoding method, Need to use parent class OutputStreamWriter

FileReader class , initialization method FileReader (file file), also if you want to set the decoding method, you need to use the parent class InputStreamReader

==> Similarly, the character stream provides for efficient reading and writing.

character Buffer stream: Bufferedwriter,bufferedreader.

* BufferedWriter

BufferedWriter writes text to the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings.

You can specify the size of the buffer, or accept the default size. In most cases, the default value is large enough.

* * Construction method

* * Common methods

Import java.io.*;p Ublic class Demo1 {public    static void Main (string[] args) throws IOException {        BufferedWriter F out = new BufferedWriter (New FileWriter (                "E:\\text.txt"));        Fout.write ("Hello, Java");        Fout.close ();    }}

* BufferedReader

BufferedReader reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows.

You can specify the size of the buffer, or you can use the default size. In most cases, the default value is large enough.

* Construction Method

* * Common methods

ReadLine () finally came out ~ NULL is returned if the end of the stream has been reached.

    @Test Public    Void Demo () throws IOException {        BufferedReader bufferedreader = new BufferedReader (New FileReader (                "E:\\text.txt"));        int ch;        while ((ch = bufferedreader.read ())!=-1)        {            System.out.print ((char) ch);        }        Bufferedreader.close ();    }

==> a special output stream that prints the flow PrintWriter.

PrintWriter: A formatted representation of the printed object to the text output stream. This class implements PrintStream all the print methods in. It does not contain methods for writing raw bytes, and for these bytes, the program should write using an encoded byte stream.

PrintStreamUnlike classes, if automatic refresh is enabled, this can only be done when one of the methods of println,printf , or format is called. Not every time a newline character is just printed. These methods use the platform-owned line delimiter concept, rather than line breaks.

Features of the PrintWriter print stream:

A: Only those who write data, no read data.

B: Can manipulate any type of data

C: You can specify whether to refresh automatically

D: can be initialized directly using the path

* * Construction method

* * Common methods

Java IO stream-character stream

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.