java 35 lines · 6 steps

Skipping the UTF-8 byte-order mark in Java

A small utility opens text files and transparently swallows a leading BOM character so readers never see it.

Explained by highlit
1public final class BomAwareReader {
2 
3 private static final char UTF8_BOM = '\uFEFF';
4 
5 private BomAwareReader() {
6 }
7 
8 public static BufferedReader open(Path path, Charset charset) throws IOException {
9 BufferedReader reader = Files.newBufferedReader(path, charset);
10 reader.mark(1);
11 int first = reader.read();
12 if (first != UTF8_BOM) {
13 reader.reset();
14 }
15 return reader;
16 }
17 
18 public static String stripBom(String text) {
19 if (!text.isEmpty() && text.charAt(0) == UTF8_BOM) {
20 return text.substring(1);
21 }
22 return text;
23 }
24 
25 public static List<String> readAllLines(Path path) throws IOException {
26 try (BufferedReader reader = open(path, StandardCharsets.UTF_8)) {
27 List<String> lines = new ArrayList<>();
28 String line;
29 while ((line = reader.readLine()) != null) {
30 lines.add(line);
31 }
32 return lines;
33 }
34 }
35}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A BOM is a real character in the stream, so you must actively read past it to keep it out of your data.
  2. 2Marking a reader before a peek read lets you rewind cleanly when the peeked character turns out to be wanted.
  3. 3Wrapping the utility in a private constructor and static methods signals it is a stateless helper, never instantiated.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Skipping the UTF-8 byte-order mark in Java — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code