3

当我在控制台中打印泰语字符时,它显示出一些奇怪的字符。

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

它正在打印������</p>

4

1 回答 1

5

假设您在 Windows 中使用 eclipse,以在控制台中启用 UTF-8(假设您的 IDE 能够使用 UTF-8 编码Windows -> Preferences -> General -> Workspace -> Test File Encoding = UTF-8):

  1. 转到Windows -> Preferences -> Java -> Installed JREs,选择 JRE 并编辑。添加-Dfile.encoding=UTF-8到默认 VM 参数(或者您可以编辑 eclipse.ini 并添加此参数,但它对我不起作用)

在此处输入图像描述

  1. 选择支持 UTF-8 的控制台字体:(Windows -> Preferences -> General -> Appearence -> Debug -> Console Font选择 Arial、Calibri 等)

在此处输入图像描述

  1. 根据需要以 UTF-8 编码形式从您的方法中删除“TIS-620”显式编码

代码:

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

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

正如评论中指出的那样,简单的 String print

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

输出:

ฝ่ายขาย</p>

于 2014-01-03T08:15:43.347 回答