如何用CP10000编码(java)编写文件?

时间:2017-08-18 19:34:52

标签: java file encoding

我需要将.csv文件从CP1252转换为CP10000编码。我使用下一个代码...

    File newFile = new File(inTargetCharsetFolder + File.separator + file.getName());

    try (
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "CP1252"));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "CP10000"))
    ) {

        String line;
        while ((line = reader.readLine()) != null) {
            writer.write(line + "\n");
        }

        return newFile;

    } catch (Exception e){
        return file;
    }

但在输出中我得到空文件。如果我使用,例如UTF-8而不是CP10000,一切都很好。

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

这是有效的" MacRoman"

File newFile = new File(inTargetCharsetFolder + File.separator + file.getName());

try (
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "CP1252"));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "MacRoman"))
) {

    String line;
    while ((line = reader.readLine()) != null) {
        writer.write(line + "\n");
    }

    return newFile;

} catch (Exception e){
    return file;
}