How do I get my java program to read Japanese (Unicode encoding) from a text file?

主宰稳场 提交于 2019-12-02 13:33:08
 BufferedReader reader = new BufferedReader(new FileReader(file));

should be

 BufferedReader reader = new BufferedReader(new InputStreamReader(
     new FileInputStream(file), StandardCharsets.UTF_8));

FileReader is an old utility class with no option to set the encoding. And that tripped you. The same holds for FileWriter that should use an OutputStreamWriter with a FileOutputStream and an encoding.

Whether this helps with the scanner, still is to see.

StringTokenizer parser = new StringTokenizer(lineToParse, "/");
if (parser.hasMoreTokens()) {
    String q = parser.nextToken();
    if (parser.hasMoreTokens()) {
        String a = parser.nextToken();
        QuizCard card = new QuizCard(q, a);
        cardList.add(card);
    }
}

or

String[] qa = lineToParse.split("/", 2);
if (qa.length == 2) {
    QuizCard card = new QuizCard(qa[0], aq[1]);
    cardList.add(card);
}

The latter would split "Kiom?/3 / 4" into {"Kiom", "3 / 4"} because of the split(-, 2).

To make code less cluttered, one can use Stream-related functionality that is available starting from Java 1.8:

import java.util.*;
import java.io.*;
class GenjiMonogatariReader{
    public static void main(String[] args){
        try(BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream("源氏物語.txt"), "UTF-8")))
        {
            br.lines()
              .forEach(System.out::println);
        } catch (Exception e) { e.printStackTrace(); }
    }
}

The 源氏物語.txt file should be UTF-8 encoded in this particular example. Modify charset's name as needed.

Do keep in mind, though, that in order to view the text in console (I'm talking about the cmd.exe window), the system must support the Japanese language. If it doesn't, the output will most likely be garbled. Even changing the default Locale with Locale.setDefault(Locale.JAPANESE); won't help.

On the other hand, running the above code from inside an IDE such as NetBeans (in my case it's ver.8.1) gives completely satisfactory results.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!