Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

encoding - Remove BOM from string in Java

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','') + "
");

But it don't work. Why?

Output of this string in win-1251 file:

?1,...SOME_TEXT_HERE

First "?" sign is illegal.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...