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:
Encoding | BOM header (hex) |
---|---|
UTF-8 | EF 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 |
Example
In the following example, a CSV file with a BOM header is created and read using FastCSV.
package example;
import static java.nio.charset.StandardCharsets.UTF_16LE;
import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.util.stream.Stream;
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 = prepareTestFile();
final CsvReader.CsvReaderBuilder builder = CsvReader.builder() .detectBomHeader(true);
try (Stream<CsvRecord> csv = builder.ofCsvRecord(testFile).stream()) { csv.forEach(System.out::println); } }
// Create a file with content encoded in UTF-16 little-endian and // a corresponding BOM header static Path prepareTestFile() throws IOException { final Path tmpFile = Files.createTempFile("fastcsv", ".csv"); tmpFile.toFile().deleteOnExit();
try (var out = Files.newOutputStream(tmpFile); var csv = CsvWriter.builder().build(out, UTF_16LE)) {
// Manually write UTF-16LE BOM header out.write(new byte[]{(byte) 0xff, (byte) 0xfe});
csv.writeRecord("a", "o", "u"); csv.writeRecord("ä", "ö", "ü"); }
return tmpFile; }
}
You also find this source code example in the FastCSV GitHub repository.