Java input, output character encoding problem

Source: Internet
Author: User

We read the text (no matter where we read it, the keyboard, the hard disk):

Try to use:

(read from HDD) InputStreamReader ir=new inputstreamreader (FileInputStream, "encode");

(read from keyboard) InputStreamReader ir=new InputStreamReader (system.in, "coding");

You can specify the encoding.

When you output text,

(output to hard drive) PrintStream ps=new PrintStream (FileInputStream, "encode");

(output to monitor) sytem.out. (You don't have to specify the encoding, the system uses the default, and the output is default.) )


We read the file (from hard disk) is this: from binary----character.

Our output file (to hard disk) is this: binary from character----.


Print on screen: character----(Specify encoding)--binary--(this process is the decoding of the console)--character (we see)

Memory read from the keyboard is this:

Character--(this process is the encoding of the system)--binary--(we specify decoding)--character (memory read)

Special, see this example below:

Class Readin

{

public static void Main (string[] args) throws IOException

{

BufferedReader bufr=new BufferedReader (

New InputStreamReader (system.in, "utf-8"));

BufferedWriter bufw=new BufferedWriter (

New OutputStreamWriter (System.out, "utf-8"));//

String Line=null;

while ((Line=bufr.readline ())!=null)

{

if ("Over". Equals (line))

Break

Bufw.write (Line.touppercase ());

Bufw.newline ();

Bufw.flush ();

}

Bufr.close ();

}

}


Program Run Result:

How are you doing

???

锟 Jin 锟?


Explanation: Hello is what we have entered.

In the process of reading, we specify uft-8 to decode,

Hello GBK code [ -60,-29,-70,-61]-according to Utf-8 to decode,---(not found in accordance with the UTF-8 rules of the data), the solution became???

The wrong character (which is the top????). )--coded according to UTF-8---[ -17,-65,-67,-17,-65,-67,-17,-65,-67]

[ -17,-65,-67,-17,-65,-67,-17,-65,-67]--in terminal, according to GBK----锟 Jin 锟?


This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://duanzhenyue.blog.51cto.com/9360091/1550134

Java input, output character encoding problem

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.