1

我的 java 程序正在从文本文件中读取 unicode。例如\uffff..从java GUI查看是没有问题的,但是当我尝试打印出来时,所有的文字都被覆盖了,是因为\u,还是任何其他方式来避免被覆盖的文字?

对不起我的英语不好..谢谢。

4

2 回答 2

2
于 2013-09-27T10:02:49.433 回答
1

As you already know, '\u' also known as Unicode escape is used to represent an international character. So as you can't enter that character from the keyboard itself, you need to use the unicode sequence to generate the character.

However, if such international characters are already there in a text file, so ofcourse you can read it. Java provides the class Charset, please refer the API at http://docs.oracle.com/javase/1.4.2/docs/api/java/nio/charset/Charset.html

You should use Reader/Writer API in Java to deal with such characters. Because it supports 16 bit character which includes all the different languages other than Alphabets and ASCII. Where as InputStream/OutputStream do support only 8 bit character.

So to read such characters you can use:

BufferedReader in = new BufferedReader(
        new InputStreamReader(new FileInputStream(file), "UTF-8"));

Here UTF-8 is the CharSet.

Similarly you can print the data. But where you print, your editor (where you print the character) must support the unicode characters.

You can also refer the below link for some more replies from different people: Read unicode text files with java

于 2013-09-27T10:09:15.680 回答