Questions about encoding in Java (character translation stream and character buffer stream)

Source: Internet
Author: User

The last time we used a byte stream, there was a character stream, the last time we talked about how to tell which stream to use, and if Notepad could read it, use a character stream, or use a byte stream. Using a character stream involves coding problems, and here's a format for the conversion stream.

OutputStreamWriter
OutputStreamWriter (outputstream OS): Default encoding, GBK
OutputStreamWriter (OutputStream os,string charsetname): Specifies the encoding.
InputStreamReader
InputStreamReader (InputStream is): Default encoding, GBK
InputStreamReader (InputStream is,string charsetname): Specify encoding

The principle is simple, and if you encode it in that way, you can decode it in whichever way you use it. Java default should be GBK, so if you find that some of the editor is open with garbled characters, then must be used in different decoding format.

We can also write a byte stream in the conversion stream. For example, Ios.write (a), write (' 97 '), this is wrong, because our conversion stream is a character stream, a char = two bytes, so we need to update the buffer with a flush. It would be nice to add ios.flush after the above statement. Summary, that is, when we use the character stream, as much as possible to use flush to refresh. Of course we can also not apply flush, because the Close method executes automatically when it is refreshed. However, when it executes, it will not be able to use the stream object when compared to flush. This should be noted.

Because our common operation uses the local default encoding, Java also gives a simplified version of the subclass: FileWriter and FileReader, methods are basically the same, unless you have special requirements for coding methods.

Summarize

IO stream
|--Byte stream
|--byte input stream
InputStream
int read (): reads one byte at a time
int read (byte[] bys): reads one byte array at a time

|--fileinputstream
|--bufferedinputstream
|--byte output stream
OutputStream
void write (int by): Write one byte at a time
void Write (byte[] bys,int index,int len): Write one part of a byte array at a time

|--fileoutputstream
|--bufferedoutputstream
|--character Stream
|--character input stream
Reader
int read (): read one character at a time
int read (char[] CHS): read one character array at a time

|--inputstreamreader
|--filereader
|--bufferedreader
String ReadLine (): reads one string at a time
|--character output stream
Writer
void write (int ch): Write one character at a time
void Write (char[] chs,int index,int len): Write one part of a character array at a time

|--outputstreamwriter
|--filewriter
|--bufferedwriter
void NewLine (): Write a line break

void write (string line): Write one string at a time

Questions about encoding in Java (character translation stream and character buffer stream)

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.