如何在java中使用中文和日文字符作为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4067628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:12:29  来源:igfitidea点击:

how to use chinese and japanese character as string in java?

javaunicodecjk

提问by sjain

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 语言。在此我必须使用一些中文、日文字符作为字符串并使用 System.out.println() 进行打印。

How can I do that?

我怎样才能做到这一点?

Thanks

谢谢

采纳答案by Thilo

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.

Java Strings 支持Unicode,所以中文和日文都没有问题。不过,可能需要告知其他工具(例如文本编辑器)和您的 OS shell。

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).

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

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

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)

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

String x = "漢字";

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):

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

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

回答by Illarion Kovalchuk

Just use it, Java Strings are fully unicode, so there should be nothing hard to just say

就用它吧,Java Strings 是完全 unicode 的,所以应该没什么难说的

System.out.println("世界您好!");

回答by Federico klez Culloca

A bit outdated but it made me wow!

有点过时,但它让我哇!

http://xahlee.org/java-a-day/unicode_in_java.html

http://xahlee.org/java-a-day/unicode_in_java.html

The article is about variable naming bu from that you can tell it's sufficient to write your chinese/japanese string directly in your source.

这篇文章是关于变量命名的,从中你可以看出直接在源代码中编写中文/日文字符串就足够了。

回答by RANDINT

One more thing to remember, the Reader should be BufferedReader, and what I mean is:

还有一件事要记住,Reader 应该是 BufferedReader,我的意思是:

BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (f), "UTF-8"));

this must be done because when you read the file, readLine() can be called:

必须这样做,因为当您读取文件时,可以调用 readLine():

while (br.readLine() != null)
{
  System.out.println (br.readLine());
}

This method is the only one that I found which can function normally because a regular Reader does not contain a non-static readLine() void method (this method does not accept anything).

这个方法是我发现的唯一一个可以正常运行的方法,因为常规 Reader 不包含非静态 readLine() void 方法(此方法不接受任何内容)。