从 Java 中的字符串中删除 BOM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26847500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:44:55  来源:igfitidea点击:

Remove BOM from string in Java

javaencodingutf-8byte-order-mark

提问by nkuhta

I have string in file, that contains BOM (from UTF-8). I want to convert this string to win-1251 and put it in file.

我在文件中有字符串,其中包含 BOM(来自 UTF-8)。我想将此字符串转换为 win-1251 并将其放入文件中。

I trying to remove BOM from string in this way:

我试图以这种方式从字符串中删除 BOM:

out.write(l.replace('\uFEFF','
?1,...SOME_TEXT_HERE
') + "\n");

But it don't work. Why?

但它不起作用。为什么?

Output of this string in win-1251 file:

此字符串在 win-1251 文件中的输出:

out.write(l.replace("\uFEFF", "") + "\n");

First "?" sign is illegal.

第一的 ”?” 标志是非法的。

回答by Jon Skeet

You're replacing the BOM with U+0000, rather than with an empty string. You should replace the BOM with the empty string, e.g.

您正在用 U+0000 代替 BOM,而不是用空字符串。您应该用空字符串替换 BOM,例如

##代码##