Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

System.out.println的编码问题

源文件格式UTF8, System.out.println( "Hello World!天" ); 在cmd中显示汉字的编码过程 ? 我没找到一个说法,可以解释下面四个试验。

(试验1.chcp936,环境变量设utf8)

E:hellojava>javac src/zhc/U8.java -d ./

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=utf-8

E:hellojava>java zhc.U8

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=utf-8
Hello World!天
Charset.defaultCharset :UTF-8

(试验2.chcp65001,环境变量UTF8)

E:minijavaProjhellojava>javac src/zhc/U8.java -d ./
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=utf-8

E:minijavaProjhellojava>java zhc.U8
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=utf-8
Hello World!天??
Charset.defaultCharset :UTF-8

(试验3.chcp936,没有环境变量)


E:hellojava>javac -encoding "utf-8" src/zhc/U8.java -d ./

E:hellojava>java zhc.U8
Hello World!天
Charset.defaultCharset :GBK

(试验4.chcp65001,没有环境变量)

E:hellojava>javac -encoding "utf-8" src/zhc/U8.java -d ./

E:hellojava>java zhc.U8
Hello World!??
Charset.defaultCharset :GBK

结论:以上4个试验都是在win7下。
在win10下的情况也不正常,版本号14393之后是这样:都用defualtCharset:UTF8这个编码,不论chcp是936还是65001都能正确显示中英混排
image
在linux下是正常的,比如在国产系统深度下,用java_tool_opt控制编码,在终端右击可以切换编码。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...