Java 将 windows-1251 rtf 解码为 utf-8

问题描述

我有一个 .rtf 文件。该文件采用 windows-1251 编码。

我需要将此字符串保存到另一个文件中,并且需要将其保存为 utf-8 编码。我需要这个文件在结果中具有良好的可读性。

所以,我尝试了很多变体,我阅读了 java-docs 和其他来源,我花了 2 天时间寻找答案,但仍然无法将其转换为易于阅读的文件

Here 是带有该字符串的文件,您可以下载该文件以运行我的测试

文件图片内容

enter image description here

Here 是我的 java 测试,您可以使用并尝试转换文件

这是我来自文件代码的简短案例

@Test
public void windows1251toUtf8() throws IOException {
    //Prepare file
    File dir = new File("/tmp/TESTS/");
    if (!dir.exists() && !dir.mkdirs()) {
        throw new RuntimeException("Cant create destination dir");
    }
    File destination = new File(dir,"test.rtf");
    if (!destination.exists() && !destination.createNewFile()) {
        throw new RuntimeException("Cant create destination file");
    }

    //-----------------------------------------------------------------------------------------

    //Not work
    InputStream inputStream = getClass().getClassLoader().getResourceAsstream("utils/encoding/windows1521File.rtf");
    Scanner sc = new Scanner(inputStream,"WINDOWS-1251");
    StringJoiner stringBuilder = new StringJoiner("\n");
    while (sc.hasNextLine()) {
        stringBuilder.add(sc.nextLine());
    }

    String text = decode(stringBuilder.toString(),"WINDOWS-1251","UTF-8");

    byte[] bytes = text.getBytes(Charset.forName("UTF-8"));

    Files.write(bytes,destination);


    //-----------------------------------------------------------------------------------------

    //Not work
    URL resource = getClass().getClassLoader().getResource("utils/encoding/windows1521File.rtf");
    String string = FileUtils.readFiletoString(new File(resource.getPath()),Charset.forName("WINDOWS-1251"));

    byte[] bytes = convertEncoding(string.getBytes(),"UTF-8");

    FileUtils.writeByteArrayToFile(destination,bytes);

    //-----------------------------------------------------------------------------------------

    //Not work
    InputStream inputStream = getClass().getClassLoader().getResourceAsstream("utils/encoding/windows1521File.rtf");

    byte[] bytes = IoUtils.toByteArray(inputStream);
    String s = new String(bytes);
    byte[] bytes2 = s.getBytes("WINDOWS-1251");

    FileUtils.writeByteArrayToFile(destination,bytes2);
}

public static byte[] convertEncoding(byte[] bytes,String from,String to) throws UnsupportedEncodingException {
    return new String(bytes,from).getBytes(to);
}

public static String decode(String text,String textCharset,String resultCharset) {
    if (StringUtils.isEmpty(text)) {
        return text;
    }

    try {
        byte[] bytes = text.getBytes(textCharset);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        byte[] tmp = new byte[bytes.length];
        int n = inputStream.read(tmp);
        byte[] res = new byte[n];
        System.arraycopy(tmp,res,n);
        return new String(res,resultCharset);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

在结果的所有情况下,我都抓住了这样的事情

enter image description here

或者像这样

enter image description here

有没有办法进行转换?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)