我有一个包含西里尔符号的 txt 文件。我是这样读的:
String csvFile = "C:\\Users\\dolgopolov.a\\Desktop\\Список рассылки 14 07 2014.txt";
BufferedReader br = null;
String line = "";
String cvsSplitBy = "\t";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] country = line.split(cvsSplitBy);
System.out.println("Номер: " + country[0]
+ " , Сообщение: " + country[1] + "");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但是输出很奇怪:
Номер: 9047120386 , Сообщение: ��������� �������! �� ����� ����� ������� ������������� � ������� 2,98 ���., ������� ���������� �������� � ������� 3 ����
那么,我该如何避免呢?我是否必须更改编码类型或其他内容?
最佳答案
FileReader
使用默认编码,因此您必须使用 InputStreamReader
new InputStreamReader(new FileInputStream(filePath), 编码)
关于java - 如何读取非 ASCII 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25037591/