如何在java中使用中文和日文字符作为字符串? [英] how to use chinese and japanese character as string in java?

查看:391
本文介绍了如何在java中使用中文和日文字符作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我使用的是java语言。在这里我必须使用一些中文,日文字符作为字符串并使用System.out.println()进行打印。

Hi
I am using java language. In this I have to use some chinese, japanese character as the string and print using System.out.println().

我该怎么做?

谢谢

推荐答案

Java Strings支持Unicode,所以中文和日文都没问题。但是,可能需要告诉其他工具(例如文本编辑器)和操作系统shell。

Java Strings support Unicode, so Chinese and Japanese is no problem. Other tools (such as text editors) and your OS shell probably need to be told about it, though.

读取或打印Unicode数据时,必须确保控制台或流也支持Unicode(否则它可能会被问号替换)。

When reading or printing Unicode data, you have to make sure that the console or stream also supports Unicode (otherwise it will likely be replaced with question marks).

Writer unicodeFileWriter = new OutputStreamWriter(
    new FileOutputStream("a.txt"), "UTF-8");
unicodeFileWriter.write("漢字");

您可以直接在Java源代码文件中嵌入Unicode文字,但是您需要告诉编译器文件是UTF-8( javac -encoding UTF-8

You can embed Unicode literals directly in Java source code files, but you need to tell the compiler that the file is in UTF-8 (javac -encoding UTF-8)

String x = "漢字";

如果你想疯狂,你甚至可以在方法,变量或类名中使用中文字符。但这是违反命名约定的,我强烈反对它至少对于类名称(因为它们需要映射到文件名,Unicode可能会导致问题):

If you want to go wild, you can even use Chinese characters in method, variable, or class names. But that is against the naming conventions, and I would strongly discourage it at least for class names (because they need to be mapped to file names, and Unicode can cause problems there):

結果 漢字 = new 物().処理();

这篇关于如何在java中使用中文和日文字符作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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