简体   繁体   English

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

[英]How to write the file in CP10000 encoding (java)?

I need to convert .csv file from CP1252 to CP10000 encoding. 我需要将.csv文件从CP1252转换为CP10000编码。 I use next code... 我使用下一个代码...

    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;
    }

But at the output I get empty file. 但是在输出中我得到了空文件。 If I use, for example UTF-8 instead CP10000, all is fine. 如果我使用例如UTF-8而不是CP10000,则一切正常。

Where is my mistake? 我的错误在哪里?

This is working "MacRoman" 这是正在工作的“ 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM