Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing BOM characters using Java [duplicate]

Tags:

What needs to happen to a string using Java to be an equivalent of vis

:set nobomb

Assume that BOM comes from the file I am reading.

like image 381
James Raitsev Avatar asked Feb 19 '14 20:02

James Raitsev


2 Answers

Java does not handle BOM properly. In fact Java handles a BOM like every other char.

Found this:

http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

public static final String UTF8_BOM = "\uFEFF";

private static String removeUTF8BOM(String s) {
    if (s.startsWith(UTF8_BOM)) {
        s = s.substring(1);
    }
    return s;
}

May be I would use apache IO instead:

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/BOMInputStream.html

like image 138
Christian Kuetbach Avatar answered Sep 23 '22 04:09

Christian Kuetbach


For UTF-8 the BOM is: 0xEF, 0xBB, 0xBF

like image 20
Theresia Sofia Snow Avatar answered Sep 22 '22 04:09

Theresia Sofia Snow