Remove BOM from string in Java
Asked Answered
T

1

8

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.

Transversal answered 10/11, 2014 at 15:46 Comment(0)
L
21

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");
Lundberg answered 10/11, 2014 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.