简体   繁体   English

从 Java 中的字符串中删除 BOM

[英]Remove BOM from string in Java

I have string in file, that contains BOM (from UTF-8).我在文件中有字符串,其中包含 BOM(来自 UTF-8)。 I want to convert this string to win-1251 and put it in file.我想将此字符串转换为 win-1251 并将其放入文件中。

I trying to remove BOM from string in this way:我试图以这种方式从字符串中删除 BOM:

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

But it don't work.但它不起作用。 Why?为什么?

Output of this string in win-1251 file:此字符串在 win-1251 文件中的输出:

?1,...SOME_TEXT_HERE

First "?"第一的 ”?” sign is illegal.标志是非法的。

You're replacing the BOM with U+0000, rather than with an empty string.您正在用 U+0000 而不是空字符串替换 BOM。 You should replace the BOM with the empty string, eg您应该用空字符串替换 BOM,例如

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

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

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