从Java中的字符串中删除BOM [英] Remove BOM from string in Java

查看:965
本文介绍了从Java中的字符串中删除BOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  out.write(l.replace('\FEFF','\0')+\\\
);

但它不工作。为什么?



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

 ?1,... SOME_TEXT_HERE 

首先?符号是非法的。

解决方案

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

  out.write(l.replace(\FFF, )+\\\
);


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.

解决方案

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");

这篇关于从Java中的字符串中删除BOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆