我担心你在问题中误传了UTF-8和ANSI.
UTF-8不需要在其开始时具有BOM.没有“UTF-8无BOM”编码这样的编码.只有“UTF-8”.我已经处理了数百万(当然,数十万)的UTF-8文件,并且从一开始就没有遇到过BOM.
根据the Unicode standard,UTF-8既不要求也不推荐BOM:
2.6 Encoding Schemes
Use of a BOM is neither required nor recommended for UTF-8, but may be encounter in contexts where UTF-8 data is converted from other encoding forms that use a BOM or where the BOM is used as a UTF-8 signature. See the “Byte Order Mark” subsection in Section 16.8, Specials, for more information.
此外,没有像“ANSI”这样的编码!
IANA provides为字符集名称提供的“ANSI”最接近的是“ANSI_X3.4-1968”和“ANSI_X3.4-1986”,它们都是“US-ASCII”(首选MIME名称)的旧别名,一个128位代码点的7位编码.没有其他官方字符集名称在其名称中包含“ANSI”.
我不确定你在什么环境下运行,但它似乎导致你进入一些非标准的命名,期望和标准.
它可能是…… Windows™? ☹
编辑:刚刚发现this answer关于这个错误的来源.