FastCSV is capable of reading CSV files with a Byte order mark
(BOM) header.
Note
A BOM header is a sequence of bytes at the beginning of a text file that indicates the file’s encoding,
such as UTF-8, UTF-16, or UTF-32.
Although UTF-8 is the default encoding for most text files today,
some applications still use the BOM header to specify the file’s encoding.
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.
Example
In the following example, a CSV file with a BOM header is created and read using FastCSV.
import static java.nio.charset.StandardCharsets.UTF_16LE ;
import java.io.IOException ;
import java.io.OutputStreamWriter ;
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 ()
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 ( new OutputStreamWriter ( out, UTF_16LE )) ) {
// Manually write UTF-16LE BOM header
out . write ( new byte []{( byte ) 0xff , ( byte ) 0xfe } ) ;
csv . writeRecord ( " a " , " o " , " u " ) ;
csv . writeRecord ( " ä " , " ö " , " ü " ) ;
You also find this source code example in the
FastCSV GitHub repository .