zoukankan      html  css  js  c++  java
  • Troubleshooting – Eclipse displaying Chinese

    Question desc:
    I am using Eclipse for java file. I write below code, and then RUN, but the result is garbage characters. How to fix it?
    ——————————
    package ch.allenstudy.newway01;
    public class TestPrintf {
    public static void main(String[] args)
    {
    // 定义一些变量,用来格式化输出。
    double d = 345.678;
    String s = “Hello world 世界”;
    int i = 1234;
    // “%”表示进行格式化输出,”%”之后的内容为格式的定义。
    System.out.printf(“%f”,d); //”f”表示格式化输出浮点数。
    System.out.println();
    System.out.printf(“%9.2f”,d); //”9.2″中的9表示输出的长度,2表示小数点后的位数。
    System.out.println();
    System.out.printf(“%+9.2f”,d); //”+”表示输出的数带正负号。
    System.out.println();
    System.out.printf(“%-9.4f”,d); //”-”表示输出的数左对齐(默认为右对齐)。
    System.out.println();
    System.out.printf(“%+-9.3f”,d); //”+-”表示输出的数带正负号且左对齐。
    System.out.println();
    System.out.printf(“%d”,i); //”d”表示输出十进制整数。
    System.out.println();
    System.out.printf(“%o”,i); //”o”表示输出八进制整数。
    System.out.println();
    System.out.printf(“%x”,i); //”x”表示输出十六进制整数。
    System.out.println();
    System.out.printf(“%#x”,i); //”#x”表示输出带有十六进制标志的整数。
    System.out.println();
    System.out.printf(“%s”,s); //”d”表示输出字符串。
    System.out.println();
    System.out.printf(“输出一个浮点数:%f,一个整数:%d,一个字符串:%s”,d,i,s);
    // 可以输出多个变量,注意顺序。
    System.out.println();
    System.out.printf(“字符串:%2$s,%1$d的十六进制数:%1$#x”,i,s);
    // “X$”表示第几个变量。
    System.out.println(“\n—————–”);
    System.out.print(“你们”);
    System.out.println(“\n—————–”);
    System.out.println(“Now is using encoding: “+System.getProperty(“file.encoding”));
    }
    }
    ————————-
    Answer:
    Let’s understand something first:
    Java normally use Unicode to save data, there are 3 steps of handling characters:
    1. Read character data with specific encoding from source;
    2. Save the character data with UNICODE into memory
    3. Ouput the character data with specific encoding.
    So Java experiences encoding conversion twice for handling characters. Frist time is converting from specific encoding to Unicode; second time is from Unicode to specific encoding.
    This is the theory we based on troubleshooting the issue.

    Be simple, let’s see below steps:
    1. Window > Preferences. In “text filter text”, input “Encod”, then will filter Workspace item.
    2. In “Text file encoding” part, select “Other”, then type GBK into the following text field directly. With this way, we have a “GBK” encoding.
    3. Click OK button.
    In fact the up steps are to adding “GBK” options for us in some places.
    4. Run > Run … > Common tab.
    5. Make sure the “Console Encoding” is also “GBK”.
    6. You also can go to File > Properties > Info. Check whether the selection is “GBK”.
    Ok, now, run it again.

    Result is as below
    ———————–
    345.678000
    345.68
    +345.68
    345.6780
    +345.678
    1234
    2322
    4d2
    0x4d2
    Hello world 世界
    输出一个浮点数:345.678000,一个整数:1234,一个字符串:Hello world 世界
    字符串:Hello world 世界,1234的十六进制数:0x4d2
    —————–
    你们
    —————–
    Now is using encoding: GBK

    Notice: there’s a System.getProperty(“file.encoding”) to judge which encoding you are using.

  • 相关阅读:
    关于Android反编译
    调用图灵机器人API实现Android智能机器人
    STM32 驱动1602液晶
    1602A液晶
    STM32 PWM波
    STM32的优先级NVIC_PriorityGroupConfig
    STM32——timer
    Ajax Json的后台例子
    固定导航例子
    本周学习总结
  • 原文地址:https://www.cnblogs.com/backpacker/p/2269960.html
Copyright © 2011-2022 走看看