Removing BOM characters using Java [duplicate]

你离开我真会死。 提交于 2019-11-26 22:00:19

问题


This question already has an answer here:

  • Byte order mark screws up file reading in Java 8 answers

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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/21891578/removing-bom-characters-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!