如何用Scanner java阅读非英语字符?

sae*_*eed 0 java subtitle java.util.scanner

在那里我正在制作这个应用程序来改变字幕文件.当我测试它时,我遇到了一个奇怪的问题,当我在非英语(例如波斯语)上测试它时程序将无法读取该文件.这就是我在程序中阅读字幕的方式:

    Scanner sub = null;
    try {
      sub = new Scanner(new File(address));
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
while(sub.hasNext()){
  String sentence = sub.nextLine();
  //some magical stuff here :)
}
Run Code Online (Sandbox Code Playgroud)

其中address是.srt文件的String保存位置.

我应该怎么做才能让程序读取文件?

Obi*_*ere 7

创建时选择不同的编码Scanner.

这可能有用:

new Scanner(new File(address), "UTF-16");
Run Code Online (Sandbox Code Playgroud)

这将更改扫描程序以使用UTF-16编码读取文件.

您可以在此处阅读有关编码的更多信息.