当我阅读带有俄文字母的“MyFile.txt”文件时,我遇到了同样的问题。可能对任何人都有帮助。解决方案是:
package j;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class J4 {
public static void Read_TXT_File(String fileName) throws
FileNotFoundException {
try{int i=0;
Scanner scanner = new Scanner(new File(fileName), "utf-8");
while (scanner.hasNext()) {
String line = scanner.nextLine();
//byte bytes[] = line.getBytes("UTF-8");
//line = new String(bytes, "UTF-8");
if (line.isEmpty()) {
System.out.println(i+": Empty line");
}
else {
System.out.println(i+": "+ line);
// here is your code for example String MyString = line
}
i++;
}
}catch(Exception ex){ex.printStackTrace();}
}
public static void main(String[] args) throws
FileNotFoundException {
Read_TXT_File("MyFile.txt");
}
}