读书人

quot;java -Dfile.encoding=GBK class 中

发布时间: 2012-03-30 17:32:09 作者: rapoo

"java -Dfile.encoding=GBK class 中文字符串" 传入的中文string 显示部正确
我的java程序:



---------------------------------------------------

import java.io.*;
import java.lang.String;

public class a
{

public static void main( String[] args )
{

try{


String wsurl = args[0].trim();
System.out.println(wsurl);
System.out.println( "111 ");
System.out.println( "文章 ");
System.out.println( "222 ");
printmultibyte(wsurl);
System.out.println( " ");
System.out.println( "333 ");
printmultibyte( "文章4 ");
System.out.println( " ");
System.out.println( "444 ");
}

catch (Exception ex)
{
System.out.println( "\nException while calling web service: ");
ex.printStackTrace();
}


}


static void printmultibyte( String file )
{

try {
String str = file;
BufferedWriter stdout = new BufferedWriter(new OutputStreamWriter(System.out, "gb2312 "));
stdout.write(str);
stdout.flush();
}
catch (java.io.IOException e)
{
System.out.println( "wrong ");
}
}

}
--------------------
步骤:
在linux环境里,
javac -encoding GBK a.java ( or java a.java)
java -Dclient.encoding.override=GBK -Dfile.encoding=GBK -Duser.language=zh -Duser.region=CN a 中
物拷锟斤拷
111

222
???
333
文章4
444
---------------------
问题:
为什么传入的中文字符串---中, 显示不正确?


[解决办法]
请查看你的source code 文件本身的编码(在ultraedit,notepad中可以看到)是否为UTF8。
在调用Javac的时候实用的-encoding的参数一定要与你的source code编码一致。在执行java的时候则不需要指定编码。

读书人网 >J2SE开发

热点推荐