A text file created under Windows with a text editor that, if saved in Unicode format such as UTF-8, adds a BOM ID to the file header (the first character).
This identifier is not removed when Java reads the file, and String.Trim () cannot be deleted. If you use ReadLine () to read the first line into a string, the length of the string is 1 larger than the one you see, and the first character is the BOM.
This situation can cause some problems, such as when reading the INI file, if you want to determine whether the first line is "[" can not be correctly judged at the beginning.
Fortunately, Java when reading Unicode files, will unify the BOM into "\ufeff", so that you can manually solve (after judging, with substring () or replace () to remove the BOM):
if (Line.startswith ("\ufeff")) { //line = line.substring (1); line = Line.replace ("\ufeff", ""); }
What is a BOM? BOM = byte order Markbom is the recommended method for labeling byte ordering in the Unicode specification. For example, for UTF-16, if the receiver receives the BOM is Feff, indicating that the byte stream is Big-endian, if received Fffe, it indicates that the byte stream is Little-endian. UTF-8 does not require a BOM to indicate byte order, but can use a BOM to indicate "I am UTF-8 encoded". The UTF-8 encoding of the BOM is the EF BB BF (Open text with UltraEdit, switch to 16 binary to see). So if the receiver receives a byte stream beginning with the EF BB BF, it knows that this is UTF-8 encoded.
All BOMs are treated as "\ufeff" in C/c++/java, reference: http://www.fileformat.info/info/unicode/char/feff/index.htm
Wikipedia Description of Pom: Https://en.wikipedia.org/wiki/Byte_order_mark
(original article, reprint please specify the blog from Clement-xu)
Copyright NOTICE: This article is the original article, reprint please indicate the CSDN blog which is transferred from Clement-xu.
Java read Unicode file (UTF-8, etc.) encountered when the BOM first character problem, and processing methods