Mac终端中汉字显示为问号 [英] Chinese Characters Displayed as Questions Marks in Mac Terminal

查看:54
本文介绍了Mac终端中汉字显示为问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Java 文件从数据库中检索一些 UTF-8 uni 编码的中文字符.当我这样做时,字符将作为问号返回.

I am trying to retrieve some UTF-8 uni coded Chinese characters from a database using a Java file. When I do this the characters are returned as question marks.

但是,当我显示数据库中的字符时(使用select * from ...),字符显示正常.当我在一个由汉字组成的Java文件中打印一个String时,它们也正常打印.

However, when I display the characters from the database (using select * from ...) the characters are displayed normally. When I print a String in a Java file consisting of Chinese characters, they are also printed normally.

我在 Eclipse 中遇到了这个问题:当我运行程序时,字符被打印为问号.不过这个问题在我以UTF-8格式保存Java文件后就解决了.

I had this problem in Eclipse: when I ran the program, the characters were being printed as question marks. However this problem was solved when I saved the Java file in UTF-8 format.

在终端中运行locale"当前返回:

Running "locale" in the terminal currently returns this:

LANG="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_CTYPE="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_ALL=

我也试过用这个来编译我的 java 文件:

I have also tried to compile my java file using this:

 javac -encoding UTF-8 [java file] 

但是,输出仍然是问号.

But still, the output is question marks.

它只是有时会显示字符,这很奇怪.有没有人对此有解释?或者更好的是,如何解决这个问题以便正确显示字符?

It's quite strange how it will only sometimes display the characters. Does anyone have an explanation for this? Or even better, how to fix this so that the characters are correctly displayed?

推荐答案

System.out 打印流不是作为 UTF-8 打印流创建的.你可以把它转换成这样:

The System.out printstream isn't created as a UTF-8 print stream. You can convert it to be one like this:

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class JavaTest {

    public static void main(String[] args) {


        try{
            PrintStream out = new PrintStream(System.out, true, "UTF-8");

            out.println("Hello");
            out.println("施华洛世奇");
            out.println("World");
        }
        catch(UnsupportedEncodingException UEE){

            //Yada yada yada
        }
    }
}

您还可以按照此处设置默认编码:

java -Dfile.encoding=UTF-8  -jar JavaTest.jar

这篇关于Mac终端中汉字显示为问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆