Home >Java >javaTutorial >Why Does the BOM Marker Appear in FileReader Output When Reading UTF-8 Encoded Files?
BOM Marker Inclusion in FileReader Output
When using a FileReader to read a UTF-8-encoded file with a BOM (Byte Order Mark), the BOM marker may inadvertently appear in the output string. This occurs because the BOM is included as part of the UTF-8 encoded representation of the text.
To understand why this happens, it's important to note that the BOM is a special character or sequence of characters that indicates the encoding of a text file. In the case of UTF-8, the BOM is represented by the byte sequence EFBBBF.
When the FileReader reads the file, it decodes the characters using the UTF-8 encoding. However, the BOM is not a valid Unicode character, so it is not skipped or removed during the decoding process. Instead, it is included in the string that is returned by the readLine() method.
To avoid this issue, you can use the following approaches:
The above is the detailed content of Why Does the BOM Marker Appear in FileReader Output When Reading UTF-8 Encoded Files?. For more information, please follow other related articles on the PHP Chinese website!