Remove BOM from string in Java

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

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.

I trying to remove BOM from string in this way:

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

But it don't work. Why?

Output of this string in win-1251 file:

?1,...SOME_TEXT_HERE 

First "?" sign is illegal.

回答1:

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.

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!