我试图将纯文本文件(UTF-8)的内容转换为ASCII(ISO-8859-15),以将其写入输出文件。我编写了几行代码(见下文),它读取文件的内容,将其写入字节数组,使用UTF-8字符集对其进行解码,使用ISO-8859-15 Charset进行编码并将结果写入文件。除了在输出文件的最开头突然出现的问号(Hx:3F)之外,这很好用。
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
public class Main {
public static void main(String[] args) {
/* Read / write the file to a byte array */
File input = new File("input.txt");
byte[] bytes = new byte[(int) input.length()];
try (FileInputStream fileInput = new FileInputStream(input)){
fileInput.read(bytes);
} catch(IOException e) {
if(e instanceof FileNotFoundException) {
System.err.println("File not found.");
} else {
e.printStackTrace();
}
}
/* Getting the charsets */
Charset utf8charset = Charset.forName("UTF-8");
Charset iso885915charset = Charset.forName("ISO-8859-15");
/* Wrapping the bytes from the file into a buffer */
ByteBuffer inputBuffer = ByteBuffer.wrap(bytes);
/* Encoding the text file from UTF-8 */
CharBuffer data = utf8charset.decode(inputBuffer);
/* Decoding the text file to ISO-8859-15 and writing it to an array*/
ByteBuffer outputBuffer = iso885915charset.encode(data);
byte[] outputData = outputBuffer.array();
System.out.println(new String(outputData));
File output = new File("output.txt");
/* Writing the output to a file */
try(BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output))) {
out.write(outputData);
out.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
输入文件:
ABC
DEF
GHI
输出文件:
?ABC
DEF
GHI
如果您有任何想法,可能是造成这种奇怪行为的原因,请告诉我。另外,如果我的代码总体上有任何奇怪之处,请指出我,因为我对Java的使用还不是很有经验。
谢谢:)