Java read Unicode file (UTF-8, etc.) encountered when the BOM first character problem, and processing methods

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.