Home  >  Article  >  Java  >  How to Handle BOM Markers in UTF-8 Encoded Files?

How to Handle BOM Markers in UTF-8 Encoded Files?

Barbara Streisand
Barbara StreisandOriginal
2024-11-19 04:47:02603browse

How to Handle BOM Markers in UTF-8 Encoded Files?

BOM Marker in UTF-8 Encoded Files

When reading a UTF-8 encoded file with a BOM (byte order mark), the BOM marker may be unintentionally included in the output string. To address this, follow these steps:

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String tmp = null;
String content = "";

while ((tmp = br.readLine()) != null) {
    String text;
    if (tmp.startsWith("\uFEFF")) {
        // Skipping the BOM marker
        text = tmp.substring(1);
    } else {
        text = tmp;
    }

    content += text + System.getProperty("line.separator");
}

In this updated code:

  • We use String.startsWith("uFEFF") to check if the current line starts with the BOM marker.
  • If it does, we remove the BOM marker by using String.substring(1).
  • If it doesn't, we use the original line as is.
  • We then append the modified line to the content string.

By removing the BOM marker before appending it to the output string, we can prevent it from appearing in the final result.

The above is the detailed content of How to Handle BOM Markers in UTF-8 Encoded Files?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn