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