如何使用Scanner Java读取非英语字符? [英] How to read non-english characters with Scanner java?
问题描述
有 我正在制作此应用来更改字幕文件. 当我测试它时,我遇到一个奇怪的问题,当我在非英语(例如,波斯语)上对其进行测试时,该程序将无法读取该文件. 这是我在程序中阅读字幕的方式:
there I'm making this app to change subtitle files. when I was testing it I faced a strange problem, when I was testing it on non-english (persian for instance) the program wouldn't read the file. this is how I read subtitles in my program:
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 :)
}
其中address是.srt文件的字符串存放位置.
where address is a String keeping place of .srt file.
我应该怎么做,程序才能读取文件?
what should I do so the program reads the file?
推荐答案
在创建Scanner
时选择其他编码.
Select a different encoding when creating the Scanner
.
类似的事情可能会起作用:
Something along the lines of this might work:
new Scanner(new File(address), "UTF-16");
这将更改扫描程序以使用UTF-16编码读取文件.
This will change the scanner to read the file using a UTF-16 encoding.
您可以在此处详细了解编码.
You can read up more on encodings here.
这篇关于如何使用Scanner Java读取非英语字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!