我需要将文件从 UTF-8 编码为 Shift_JIS。以前,这是使用 iconv 命令完成的,如下所示
iconv -f utf8 -t sjis $INPUT_FILE
我提供的输入文件返回一个错误,说
位置 2551 处的非法输入序列
我写了这段Java代码:
FileInputStream fis = new FileInputStream(
"Input.txt");
InputStreamReader in = new InputStreamReader(fis, "UTF-8");
FileOutputStream fos = new FileOutputStream("Output.txt");
OutputStreamWriter out = new OutputStreamWriter(fos, "Shift_JIS");
int val = 0;
StringBuilder sb = new StringBuilder();
while((val =in.read() )!= -1){
System.out.println(Integer.toHexString(val));
sb.append((char)val);
}
out.write(sb.toString());
out.flush();
fis.close();
out.close();
该代码使用相同的输入文件执行良好,并且不返回任何错误。
我在这里错过了什么吗?
约阿希姆。这看起来像答案。我在问题中添加了我的代码。我现在收到不可映射的字符错误。但它无法像任何文本“hello”那样对普通字符进行编码。我在任何地方都做错了吗
private static CharsetDecoder decoder(String encoding) {
return Charset.forName(encoding).newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
}
private static CharsetEncoder encoder(String encoding) {
return Charset.forName(encoding).newEncoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
}
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(
"D:\\Input.txt");
InputStreamReader in = new InputStreamReader(fis, decoder("UTF-8"));
FileOutputStream fos = new FileOutputStream("D:\\Output.txt");
OutputStreamWriter out = new OutputStreamWriter(fos, encoder("Shift_JIS"));
char[] buffer = new char[4096];
int length;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
out.flush();
}