2

为什么这两个代码块会产生不同的字节数组?

Apache Commons IO 2.4:

Charset iso_8859_1 = Charset.availableCharsets().get("ISO-8859-1");
return IOUtils.toByteArray(new FileReader(getResourceFile(filename)), iso_8859_1);

JDK:

Charset iso_8859_1 = Charset.availableCharsets().get("ISO-8859-1");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(baos, iso_8859_1));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getResourceFile(filename)), iso_8859_1));
String line;
while ((line = reader.readLine()) != null)
    writer.write(line);

writer.flush();
return baos.toByteArray();

告诉我我在看鬼...

4

1 回答 1

1

尝试使用 FileInputStream,因为无法使用 FileReader 指定字符集。

IOUtils.toByteArray(new InputStreamReader(new FileInputStream(getResourceFile(filename)), "ISO-8859-1"), "ISO-8859-1")              
于 2012-09-03T07:41:22.910 回答