gpt4 book ai didi

java 8写utf-8编码乱码

转载 作者:行者123 更新时间:2023-11-30 05:51:35 26 4
gpt4 key购买 nike

我正在尝试获取一个可用的 utf-8 文本文件。我所说的工作是指它应该在 Firefox 和 libreoffice 中正确显示。

要写我使用:

PrintWriter writer = new PrintWriter(savepath, "UTF-8");

为了用java阅读,我使用:

      InputStreamReader isr = new InputStreamReader(new FileInputStream(loadpath), Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);

再次读入并在java中显示时,它是正确的。

使用 libreoffice 或 firefox(编码设置为 unicode),它会显示“乱码”字符。

示例文本:

C'est vrai   (Java) The french "That's true" is shown correct

Cest vrai (firefox -set to unicode) the "'" becomes a square with "0092" inside

编辑:

CÂ’est vrai (firefox -set to middle european (windows) encoding)

编辑2:

writer.println("Cest vrai");
writer.flush();

https://imgur.com/a/CLL3eVK

编辑3:好的,我现在尝试使用

writer.println("C"+"\u0092"+" est vrai");
writer.flush();

这会导致(firefox 设置为 unicode): https://imgur.com/qGRr9sC

在 libreoffice calc import 中,它看起来甚至有点不同(方 block 是空的): https://imgur.com/MNQNDWo

最佳答案

你掉进了一个不那么明显的陷阱。 U+0092 不是有效的 Unicode 字符。 Windows 1252接近Latin1,但添加了一些字符。例如,右单引号 (') 是 U+2019,但在 Windows 1252 字符集中映射到 '\x92',并且在真正的 Latin1 中根本不可用.

如果你想用Java正确编写它,你应该使用“C\u2019est vrai”

这意味着编码操作是正确的,但是unicode字符是错误的。 Firefox 正确地将 0092 显示为一个不存在的 unicode 字符。

<小时/>

经过更多研究,我可以看出代码在 0x80 到 0x9f 之间的字符是危险。 0x81、0x8d、0x8f、0x90 和 0x9d 在 Windows 1252 字符集中根本不映射,所有其他字符都映射到 256 以上的 Unicode 字符。下面列出了 Windows 1252 字符集中的代码、相应的 unicode 值、字符和它的名字(感谢 Python unicodedata 模块)

Different € 0x20ac 0x80 EURO SIGN
Not mapped 0x81
Different ‚ 0x201a 0x82 SINGLE LOW-9 QUOTATION MARK
Different ƒ 0x192 0x83 LATIN SMALL LETTER F WITH HOOK
Different „ 0x201e 0x84 DOUBLE LOW-9 QUOTATION MARK
Different … 0x2026 0x85 HORIZONTAL ELLIPSIS
Different † 0x2020 0x86 DAGGER
Different ‡ 0x2021 0x87 DOUBLE DAGGER
Different ˆ 0x2c6 0x88 MODIFIER LETTER CIRCUMFLEX ACCENT
Different ‰ 0x2030 0x89 PER MILLE SIGN
Different Š 0x160 0x8a LATIN CAPITAL LETTER S WITH CARON
Different ‹ 0x2039 0x8b SINGLE LEFT-POINTING ANGLE QUOTATION MARK
Different Π0x152 0x8c LATIN CAPITAL LIGATURE OE
Not mapped 0x8d
Different Ž 0x17d 0x8e LATIN CAPITAL LETTER Z WITH CARON
Not mapped 0x8f
Not mapped 0x90
Different ‘ 0x2018 0x91 LEFT SINGLE QUOTATION MARK
Different ’ 0x2019 0x92 RIGHT SINGLE QUOTATION MARK
Different “ 0x201c 0x93 LEFT DOUBLE QUOTATION MARK
Different ” 0x201d 0x94 RIGHT DOUBLE QUOTATION MARK
Different • 0x2022 0x95 BULLET
Different – 0x2013 0x96 EN DASH
Different — 0x2014 0x97 EM DASH
Different ˜ 0x2dc 0x98 SMALL TILDE
Different ™ 0x2122 0x99 TRADE MARK SIGN
Different š 0x161 0x9a LATIN SMALL LETTER S WITH CARON
Different › 0x203a 0x9b SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
Different œ 0x153 0x9c LATIN SMALL LIGATURE OE
Not mapped 0x9d
Different ž 0x17e 0x9e LATIN SMALL LETTER Z WITH CARON
Different Ÿ 0x178 0x9f LATIN CAPITAL LETTER Y WITH DIAERESIS

关于java 8写utf-8编码乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53811846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com