ANSI XML 文档中的 BOM 究竟是什么,应该将其删除吗? XML 文档应该使用 UTF-8 吗?谁能告诉我一个可以检测 BOM 的 Java 方法? BOM 由字符 EF BB BF 组成。
最佳答案
对于 ANSI XML 文件,它实际上应该被删除。如果你想使用 UTF-8,你真的不需要它。只有 UTF-16 和 UTF-32 才需要。
The Byte-Order-Mark (or BOM), is a special marker added at the very beginning of an Unicode file encoded in UTF-8, UTF-16 or UTF-32. It is used to indicate whether the file uses the big-endian or little-endian byte order. The BOM is mandatory for UTF-16 and UTF-32, but it is optional for UTF-8.
(来源:https://www.opentag.com/xfaq_enc.htm#enc_bom)
关于如何在java中检测到这个问题。
请检查此问题的以下答案:Java : How to determine the correct charset encoding of a stream如果您现在想自己确定 BOM(风险自负),例如查看此代码 Java Tip: How to read a file and automatically specify the correct encoding .
基本上只是自己读取前几个字节,然后确定您是否可能找到了 BOM。
关于java - 什么是 XML BOM 以及如何检测它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1772321/