Skip to content

Byte order mark (BOM) header

FastCSV is capable of reading CSV files with a Byte order mark (BOM) header.

Enabling automatic BOM header detection can impact performance. Due to the decreased usage of BOM headers in modern applications, BOM header detection is disabled by default. To enable BOM header detection, use the detectBomHeader method of the CsvReaderBuilder class.

You may also want to check out the corresponding Javadoc for more information.

The following table shows the BOM headers for different Unicode encodings that FastCSV can detect:

EncodingBOM header (hex)
UTF-8EF BB BF
UTF-16 (BE)FE FF
UTF-16 (LE)FF FE
UTF-32 (BE)00 00 FE FF
UTF-32 (LE)FF FE 00 00

The UTF-8 BOM header is the most common one that is also used by Microsoft Excel.

Example

In the following example, a CSV file with a BOM header is created and read using FastCSV.

ExampleCsvReaderWithBomHeader.java
package example;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRecord;
import de.siegmar.fastcsv.writer.CsvWriter;
/// Example for reading CSV files with a BOM header.
public class ExampleCsvReaderWithBomHeader {
public static void main(final String[] args) throws IOException {
final Path testFile = createTempFile();
writeTestFile(testFile);
readTestFile(testFile);
}
static Path createTempFile() throws IOException {
final Path tmpFile = Files.createTempFile("fastcsv", ".csv");
tmpFile.toFile().deleteOnExit();
return tmpFile;
}
static void writeTestFile(final Path file) throws IOException {
try (var out = Files.newOutputStream(file);
var csv = CsvWriter.builder().build(out, UTF_8)) {
// Manually write UTF-8 BOM header
out.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
csv.writeRecord("a", "o", "u");
csv.writeRecord("ä", "ö", "ü");
}
}
static void readTestFile(final Path file) throws IOException {
final CsvReader.CsvReaderBuilder builder = CsvReader.builder()
.detectBomHeader(true);
try (CsvReader<CsvRecord> csv = builder.ofCsvRecord(file)) {
csv.forEach(System.out::println);
}
}
}

You also find this source code example in the FastCSV GitHub repository.