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:
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!