20

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.

fge
  • 110,072
  • 26
  • 223
  • 312
James Raitsev
  • 82,013
  • 132
  • 311
  • 454
  • 6
    Strings in Java do not have BOM... Unless you read from a source which has one – fge Feb 19 '14 at 20:24
  • 2
    This is precisely what happens. I am reading the file that happens to have this mark – James Raitsev Feb 19 '14 at 20:26
  • 1
    Do you at least know what encoding is used (UTF-8, UTF-16 LE/BE)? – fge Feb 19 '14 at 20:27
  • If you have the option just open the file with Notepad++ or SublimeText and resave it without a BOM. Otherwise you'd need to know the encoding type to do it programatically – Durandal Feb 19 '14 at 20:28

2 Answers2

63

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

Christian Kuetbach
  • 15,171
  • 4
  • 37
  • 74
  • 2
    `UTF8_BOM` is a wrong name. There is nothing in the BOM that links it to UTF-8. On the contrary, UTF-8 does NOT need the BOM, while UTF-16 MAY (and Microsoft has the bad habit of writing UTF-16 files with a BOM, which often get converted to UTF-8 with BOM by bad tools). – Walter Tross Jan 17 '18 at 22:56
  • UTF-8 BOM consists of 3 bytes, not 2. – Krzysztof Tomaszewski Jan 23 '19 at 10:32
12

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

Theresia Sofia Snow
  • 579
  • 1
  • 5
  • 16