In-depth understanding of Java I/O series three: the character stream explanation

Source: Internet
Author: User
Tags save file

Why a character stream exists

Since the byte stream provides the ability to handle any type of input/output operation, why is there a character stream? Let me slow down. The byte stream cannot manipulate Unicode characters directly, because a character has two bytes, and the byte stream can operate only one bytes at a time . If Java cannot manipulate characters directly, I will feel Java is full of malice to the world, so it is necessary to provide support for direct character input/output, because our slogan is: Write once, run around.

The concept of character streams

Output character stream: The sequence of characters to be written to the file (which is actually a sequence of Unicode symbols) to the specified encoding, which is then written to the file.

Input character stream: Writes the sequence of bytes to be read to the corresponding sequence of characters (actually a sequence of Unicode symbols), to be written in memory.

Hierarchical relationships of character streams

The top level of the character stream hierarchy is the reader and writer abstract classes, which correspond to InputStream and outputstream in the byte stream.

The following is a feature that writes a string to a file and then reads it from the file in the console to familiarize yourself with some of the common streams in the character stream family through this demo.

First write the string to the file:

 Public Static void throws IOException    {        new fileoutputstream ("D:/data.txt");         New OutputStreamWriter (FIS);         New BufferedWriter (OSW);         = "China Mobile reading base"        ; = "China Mobile video base";        Bw.write (STR1);        Bw.write ("\ r \ n");        Bw.write (STR2);                Bw.close ();    }

Execution Result:

Then read the print from the file in the console

1FileOutputStream fos =NewFileOutputStream ("D:/data.txt");2OutputStreamWriter OSW =NewOutputStreamWriter (FOS, "Utf-8");3BufferedWriter BW =NewBufferedWriter (OSW);4String str1 = "China Mobile reading base";5String str2 = "China Mobile video Base";6 Bw.write (str1);7Bw.write ("\ r \ n");8 Bw.write (str2);9         Ten bw.close (); One          AFileInputStream FIS =NewFileInputStream ("D:/data.txt"); -InputStreamReader ISR =NewInputStreamReader (FIS, "UTF-8"); -BufferedReader br =NewBufferedReader (ISR); the String str; -          while(NULL! = (str =Br.readline ())) -         { - System.out.println (str); +         } - br.close (); +     

Execution Result:

China Mobile Reading base China Mobile video base

1, the BufferedWriter, BufferedReader and the bufferedinputstream in the word stream, bufferedoutputstream, the function principle is similar, does not carry on the introduction.

2, the 2nd line, the 13th line of code is the function of the byte output stream, byte input stream through the coding method, converted into a character output stream, character input stream.

New OutputStreamWriter (FOS, "utf-8");
New InputStreamReader (FIS, "UTF-8");

the OutputStreamWriter, InputStreamReader, is the two conversion streams provided in the input/output system for converting byte streams into a character stream.

You can think of the question: Why not convert the character stream into a byte-flow converter?

First of all, the difference between a word stream and the character stream: the word stream is more widely used than the character stream, but the character stream is more convenient than the byte stream. If a stream is already a character stream, it means that it is a more convenient stream to use, and why convert it into a byte stream? Conversely, if there is now a byte stream, but it can be determined that the content of the stream is text content, we can convert it into a character stream to handle it more convenient. Therefore, Java provides only the conversion of a byte stream to a stream of characters, and does not provide a conversion of the character flow to byte streams.

The above demo is implemented by byte-to-character conversion streams, so let's see if we can do this by reading the characters in the text directly:

1  Public Static voidMain (string[] args)throwsIOException2     {3FileWriter FW =NewFileWriter ("D:data.txt");4BufferedWriter BW =NewBufferedWriter (FW);5String str1 = "China Mobile reading base";6String str2 = "China Mobile video Base";7 Bw.write (str1);8Bw.write ("\ r \ n");9 Bw.write (str2);Ten bw.close (); One          AFileReader FR =NewFileReader ("D:data.txt"); -BufferedReader br =NewBufferedReader (FR); - String str; the          while(NULL! = (str =Br.readline ())) -         { - System.out.println (str); -         } + br.close (); -          +}

Execution Result:

China Mobile Reading base China Mobile video base

As we can see, the text file can be manipulated directly by Filewriter/filereader.

1. Use FileReader or BufferedReader to read character or text data from a file, and always specify character encoding, and use FileInputStream to read the original byte stream from a file or socket in Java.

2, because BufferedReader has a ReadLine method, it can be very convenient to read one line at a time, so often the input stream to read the contents of the file is packaged into BufferedReader, to facilitate reading the text content of the input stream.

Knowledge Point Tips

Computer files are often divided into text files and binary files in two major categories:

1, we may as well be able to think absolutely: all can open with Notepad and see that the character content file is called a text file, and vice versa is a binary file.

2, in fact, all the files in the computer are binary files, text is just a special existence of binary files. If the contents of a binary file are exactly parsed into characters, the binary file can be called a text file.

3. In some cases, text files are opened with the wrong character set, and garbled characters are generated. So if you are working with a text file, you must use the same character set as the save file when you open the file.

In-depth understanding of Java I/O series three: the character stream explanation

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.