java中文乱码问题

concisedistinct 2012-02-09 07:17:40
中文文字、java代码、工程均使用UTF-8编码,在Myeclipse控制台中中文显示正常,但是打包后执行则是乱码。。不清楚为什么。。谢谢。

java代码:

public class EncodingTest {
protected static Logger logger = Logger.getLogger(EncodingTest.class.getName());
/**
* @param args
*/
public static void main(String[] args) {
try {
String str=new String(("你好").getBytes(),"UTF-8");
logger.info(str);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}




Myeclipse控制台中:



打包后执行:
...全文
463 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgycsmb 2013-05-02
  • 打赏
  • 举报
回复
统一全设成uft-8
concisedistinct 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 jiakai0419 的回复:]
引用 18 楼 concisedistinct 的回复:

基本清楚了,,eclipse默认编码UTF-8,win系统中jdk默认编码GBK,并且该默认编码修改file.encoding以及Charset.forName("UTF-8")均修改不了默认编码。。但是和eclipse使用的是一个jdk,不清楚eclipse是怎么修改的。。
Charset.defaultCharset()
pu……
[/Quote]
噢。是在socket间进行数据通信。
jiakai0419 2012-02-11
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 concisedistinct 的回复:]

基本清楚了,,eclipse默认编码UTF-8,win系统中jdk默认编码GBK,并且该默认编码修改file.encoding以及Charset.forName("UTF-8")均修改不了默认编码。。但是和eclipse使用的是一个jdk,不清楚eclipse是怎么修改的。。
Charset.defaultCharset()
public static Charset defaultChar……
[/Quote]

就没必要白费二遍事。

解码再编码。玩呢?
concisedistinct 2012-02-11
  • 打赏
  • 举报
回复
基本清楚了,,eclipse默认编码UTF-8,win系统中jdk默认编码GBK,并且该默认编码修改file.encoding以及Charset.forName("UTF-8")均修改不了默认编码。。但是和eclipse使用的是一个jdk,不清楚eclipse是怎么修改的。。
Charset.defaultCharset()
public static Charset defaultCharset()Returns the default charset of this Java virtual machine.
The default charset is determined during virtual-machine startup and typically depends upon the locale and charset of the underlying operating system.

所以最好不使用str.getBytes(),太依赖系统编码了。而是使用str.getBytes(“UTF-8”)
haiyangzhisheng 2012-02-11
  • 打赏
  • 举报
回复
换台电脑试试,实在找不到问题那就有可能是系统的原因
concisedistinct 2012-02-11
  • 打赏
  • 举报
回复
使用javac -encoding UTF-8 EncodingTest.java重新编译后运行,和eclipse打包后运行结果一样

[Quote=引用 9 楼 gaowen_han 的回复:]
You could try to generate the .class files directly via the CMD. (reference: http://blog.csdn.net/gaowen_han/article/details/7163775)
To verify that the execution of the compiled file does not appear……
[/Quote]
concisedistinct 2012-02-11
  • 打赏
  • 举报
回复

恩,对,Eclipse里面运行正常。eclipse默认字符集是UTF-8。使用Charset.defaultCharset()可以查看。
[Quote=引用 6 楼 jiakai0419 的回复:]
String str=new String(("你好").getBytes(),"UTF-8");

你写成这样,在Eclipse里面运行完正常?

你不是跟我开玩笑吧。

我先给你解释下。

"你好".getBytes()的意思是按照默认的字符集给字符进行编码

一般情况下我们的电脑,默认的都是gbk

new String(byte[],"UTF-8")

的意思是……
[/Quote]
萝asdf 2012-02-10
  • 打赏
  • 举报
回复
logger.info(str);
日志文件的字符集设置了么?这个也要设置成utf-8,否则就算你是以utf-8保存的,打开后显示的还是乱码
concisedistinct 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zgycsmb 的回复:]
所有用到的地方,全用uft-8
jf
[/Quote]
恩,是的。所有文件编码使用UTF-8,工程设置UTF-8,myeclipse中显示中文正常,但打包执行显示乱码。
concisedistinct 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stocker0073 的回复:]
TOMCAT也要设置为UTF-8
[/Quote]
不部署到tomcat,使用打包jar输出时是乱码。
qqlwq123 2012-02-09
  • 打赏
  • 举报
回复
LZ你那个字符串在控制台能正常显示?你系统编码都是utf-8啊?
zgycsmb 2012-02-09
  • 打赏
  • 举报
回复
所有用到的地方,全用uft-8
jf
liyulong01 2012-02-09
  • 打赏
  • 举报
回复
utf8是世界通用的
xkjflyzz 2012-02-09
  • 打赏
  • 举报
回复
是不是设置下系统环境变量哦!估计系统识别不了UTF-8
江湖一尾鱼 2012-02-09
  • 打赏
  • 举报
回复
TOMCAT也要设置为UTF-8
fearlessMore 2012-02-09
  • 打赏
  • 举报
回复
You could try to generate the .class files directly via the CMD. (reference: http://blog.csdn.net/gaowen_han/article/details/7163775)
To verify that the execution of the compiled file does not appear the irrecognizable codes. And then pack the .class files into .jar file through CMD. (reference: http://blog.csdn.net/gaowen_han/article/details/7246562)

We can do also a small test: run via CMD the class file extracted from the eclipse workspace directory to examine there are no irrecognizable codes.
jiakai0419 2012-02-09
  • 打赏
  • 举报
回复
这么操作本身语义就有问题。

手工转码一般都用在,编解码错误的时候。

而"你好"本来就是你想要的串。干嘛还要人工转码啊。


用人工转码的时候是这样的。

就好比,现在有一个非java程序。他向你的程序发送一个字符串,传送前首先需要进行编码。他按gbk编码后给你的java程序。但是你输入流接受的时候却用了iso8859-1进行解码。于是,你看到的串就不是。他发送时候的了。这个时候你才需要new String(getBytes("iso8859-1"),"gbk").这时候才是需要人工转码的时候。
jiakai0419 2012-02-09
  • 打赏
  • 举报
回复
String str=new String(("你好").getBytes(),"UTF-8");

这么操作太另类了。

哥们,你需求是啥啊?
jiakai0419 2012-02-09
  • 打赏
  • 举报
回复
String str=new String(("你好").getBytes(),"UTF-8");

你写成这样,在Eclipse里面运行完正常?

你不是跟我开玩笑吧。

我先给你解释下。

"你好".getBytes()的意思是按照默认的字符集给字符进行编码

一般情况下我们的电脑,默认的都是gbk

new String(byte[],"UTF-8")

的意思是把该字节数组按照规定字符集(这里是UTF-8)解码。

如果,你这个串没进行过任何操作直接输出肯定是乱码。

周靖峰 2012-02-09
  • 打赏
  • 举报
回复
String str=new String(("你好").getBytes(),"UTF-8");

应该改成

String str=new String(("你好").getBytes("UTF-8"), "UTF-8");

貌似这样写没有意义吧

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧