Java : In console Thai text is printing as some strange character

心已入冬 提交于 2019-12-12 15:44:25

问题


When I am printing thai character in console it is showing some strange character.

public static void main(String[] args) throws Exception{
        byte[] bytes = "ฝ่ายขาย".getBytes("TIS-620");
        String str =  new  String(bytes);
        System.out.println(str);
}

It is printing ���¢��


回答1:


Assuming you're using eclipse in Windows, to enable UTF-8 in console (given that your IDE is able to use UTF-8 encoding Windows -> Preferences -> General -> Workspace -> Test File Encoding = UTF-8 ):

  1. Go to Windows -> Preferences -> Java -> Installed JREs, select the JRE and Edit. Add -Dfile.encoding=UTF-8 to Default VM arguments (alternatively you can edit your eclipse.ini and add this argument but it doesn't work for me)

  1. Select a UTF-8 supporting console font: Windows -> Preferences -> General -> Appearence -> Debug -> Console Font (select Arial, Calibri etc)

  1. Remove "TIS-620" explicit encoding from your method as you need it in UTF-8 encoded form

Code:

public static void main(String[] args) throws Exception {
        byte[] bytes = "ฝ่ายขาย".getBytes();

        String str = new String(bytes);
        System.out.println(str);
    }

Which is, as pointed out in the comments, simple String print

System.out.println("ฝ่ายขาย");

Output:

ฝ่ายขาย



来源:https://stackoverflow.com/questions/20898904/java-in-console-thai-text-is-printing-as-some-strange-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!