How to use UTF-8 character in Netbeans

2019-05-03 08:16发布

问题:

I am using Netbeans6.9.1 IDE and wants to show the Chinese characters in the output console using java. I copied the Chinese charater from a web page and copied between the "". but its not supported.

         String char1="世界你好";
         System.out.println(char1);

Do I need to do some setting in IDE or use some settings in my Java code?

回答1:

Edit the file netbeans.conf from the etc directory located at the installation directory of NetBeans. In the netbeans_default_options option, add -J-Dfile.encoding=UTF-8.

Works for both console input and output in NetBeans.



回答2:

Can you try this instead:

String char1="\u4e16\u754c\u4f60\u597d";
System.out.println(char1);

The escape sequences get resolved by the javac compiler to the corresponding unicode codepoints, this way you are independent of the actual source code encoding. Any remaining display problems should then be caused by the console or an incomplete font.

PS: In my Netbeans installation (7.0 M2 on Ubuntu Linux) both strings mostly work except for the third character, which gets displayed as a box.



回答3:

You can check this resource for help on setting character encoding

FaqI18nProjectEncoding



回答4:

You have to see the character encoding used by your console. Mine works fine though. Another thing to check is the character encoding used by the class file u r implementing.

Project Properties | Sources | Encoding

Set it to UTF8