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

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

问题描述

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

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文件中打印字符串时,它们也会正常打印。

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天全站免登陆