我已经创建了从 csv 文件中读取数据的代码。但是,我无法处理 £ 等特殊字符。
例如,My Base Cost (K£)
被读作 My Base Cost (K£)
。
我该怎么做才能纠正这个问题?
public void parseCSVFile(String filename){
try {
br = new BufferedReader(new FileReader(csvDirectory + filename));
while ((parsedLines = br.readLine()) != null) {
String[] parsedData = parsedLines.split(csvSplitByComma);
entireFeed.add(parsedData[0]);
entireFeed.add(parsedData[1]);
System.out.println(parsedData[0]);
System.out.println(parsedData[1]);
it = entireFeed.iterator();
}
} catch (Exception e) {
e.printStackTrace();
}
}
最佳答案
编写您的 CSV 的代码已损坏。它以 UTF-8 对其编写的文本进行三重编码。
在 UTF-8 中,ASCII 字符(代码点 0-127)表示为单个字节;他们不需要编码。这就是为什么只有 £
受到影响。
£
在 UTF-8 中需要两个字节。这些字节是:0xc2、0xa3。如果编写 CSV 文件的代码正确使用了 UTF-8,则该字符将在文件中显示为这两个字节。
但是,显然,某处的某些代码使用单字节字符集(如 ISO-8859-1)读取文件,导致每个单独的字节都被视为其自身的字符。然后它使用 UTF-8 对这些单独的字符进行编码。意思是,它采用了 { 0xc2, 0xa3 } 字节并以 UTF-8 编码它们中的每一个。这反过来又产生了这些字节:0xc3、0x82、0xc2、0xa3。 (具体:U+00C2字符在UTF-8中表示为0xc3 0x82,U+00A3字符在UTF-8中表示为0xc2 0xa3。)
然后,在那之后的某个时候,又做了同样的事情。这四个字节是使用单字节字符集读取的,每个字节都被视为自己的字符,这四个字符中的每一个以 UTF-8 编码,结果为八个字节:0xc3、0x83、0xc2、0x82、0xc3、0x82、0xc2、0xa3。 (当编码为 UTF-8 时,并非每个字符都转换为两个字节;只是碰巧所有这些字符都是。)
这就是为什么当您使用 ISO-8859-1 字符集读取文件时,每个字节得到一个字符:
à ƒ  ‚ à ‚  £
c3 83 c2 82 c3 82 c2 a3
(从技术上讲,‚
实际上是 U+201A“单低位 9 引号”,但许多每个字符一个字节的 Windows 字体历史上在位置 0x82 处有该字符。)
那么,现在我们知道您的文件是如何以这种方式获取的,您将如何处理?
首先,不要让它变得更糟。如果您可以控制写入文件的代码,请确保该代码明确指定用于读取和写入的字符集。 UTF-8 几乎总是最佳选择,至少对于主要使用西方字符的任何文件而言。
其次,如何修复文件?恐怕没有办法自动检测这种错误编码,但至少对于这个文件,您可以对其进行三重解码。
如果文件不是很大,直接读入内存即可:
byte[] bytes = Files.readAllBytes(Paths.get(csvDirectory, filename));
// First decoding: £ is represented as four characters
String content = new String(bytes, "UTF-8");
bytes = new byte[content.length()];
for (int i = content.length() - 1; i >= 0; i--) {
bytes[i] = (byte) content.charAt(i);
}
// Second decoding: £ is represented as two characters
content = new String(bytes, "UTF-8");
bytes = new byte[content.length()];
for (int i = content.length() - 1; i >= 0; i--) {
bytes[i] = (byte) content.charAt(i);
}
// Third decoding: £ is represented as one character
content = new String(bytes, "UTF-8");
br = new BufferedReader(new StringReader(content));
// ...
如果它是一个大文件,您将希望以字节为单位读取每一行:
try (InputStream in = new BufferedInputStream(
Files.newInputStream(Paths.get(csvDirectory, filename)))) {
ByteBuffer lineBuffer = ByteBuffer.allocate(64 * 1024);
int b = 0;
while (b >= 0) {
lineBuffer.clear();
for (b = in.read();
b >= 0 && b != '\n' && b != '\r';
b = in.read()) {
lineBuffer.put((byte) b);
}
if (b == '\r') {
in.mark(1);
if (in.read() != '\n') {
in.reset();
}
}
lineBuffer.flip();
byte[] bytes = new byte[lineBuffer.limit()];
lineBuffer.get(bytes);
// First decoding: £ is represented as four characters
String parsedLine = new String(bytes, "UTF-8");
bytes = new byte[parsedLine.length()];
for (int i = parsedLine.length() - 1; i >= 0; i--) {
bytes[i] = (byte) parsedLine.charAt(i);
}
// Second decoding: £ is represented as two characters
parsedLine = new String(bytes, "UTF-8");
bytes = new byte[parsedLine.length()];
for (int i = parsedLine.length() - 1; i >= 0; i--) {
bytes[i] = (byte) parsedLine.charAt(i);
}
// Third decoding: £ is represented as one character
parsedLine = new String(bytes, "UTF-8");
// ...
}
}
关于Java - 无法在 BufferedReader 中正确读取特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40634205/