我正在读取 java 中的 csv 文件,添加一个包含新信息的新列并将其导出回 CSV 文件。我在读取 UTF-8 格式的 CSV 文件时遇到问题。我逐行读取并将其存储在 a 中StringBuilder
,但是当我打印该行时,我可以看到我正在读取的信息不是 UTF-8 而是 ANSI。我使用了UTFSystem.out.print
和printstream
UTF,信息仍然显示在 ANSI 中。这是我的代码:
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
"./users.csv"), "UTF8"));
String line;
while ((line = br.readLine()) != null) {
if (line.contains("none@none.com")) {
continue;
}
if (!line.contains("@") && !line.contains("FirstName")) {
continue;
}
PrintStream ps = new PrintStream(System.out, true, "UTF-8");
ps.print(line + "\n");
sbusers.append(line);
sbusers.append("\n");
sbusers2.append(line);
sbusers2.append(",");
}
br.close();
} catch (IOException e) {
System.out.println("Failed to read users file.");
} finally {
}
它打印出诸如“Professor -P�s”之类的信息。由于读取未正确完成,新文件的输出也以 ANSI 格式导出。