Skip to content

Commit ced521a

Browse files
committed
add Excel to the BOM example
1 parent ed8f9c5 commit ced521a

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

docs/src/content/docs/guides/Examples/byte-order-mark.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ that serves as a header to indicate the file's Unicode encoding, such as UTF-8,
1313
For UTF-16 and UTF-32, the BOM header also indicates the byte order (big-endian or little-endian).
1414

1515
While UTF-8 is the standard encoding for most text files today,
16-
some applications still use the BOM header to explicitly specify the file's encoding.
16+
some applications (like Microsoft Excel) still use the BOM header to explicitly specify the file's encoding.
1717
:::
1818

1919
Enabling automatic BOM header detection can impact performance.
@@ -34,6 +34,8 @@ The following table shows the BOM headers for different Unicode encodings that F
3434
| UTF-32 (BE) | `00 00 FE FF` |
3535
| UTF-32 (LE) | `FF FE 00 00` |
3636

37+
The UTF-8 BOM header is the most common one that is also used by Microsoft Excel.
38+
3739
## Example
3840

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

example/src/main/java/example/ExampleCsvReaderWithBomHeader.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package example;
22

3-
import static java.nio.charset.StandardCharsets.UTF_16LE;
3+
import static java.nio.charset.StandardCharsets.UTF_8;
44

55
import java.io.IOException;
66
import java.nio.file.Files;
@@ -25,17 +25,16 @@ public static void main(final String[] args) throws IOException {
2525
}
2626
}
2727

28-
// Create a file with content encoded in UTF-16 little-endian and
29-
// a corresponding BOM header
28+
// Create a file with UTF-8 encoding and a corresponding BOM header
3029
static Path prepareTestFile() throws IOException {
3130
final Path tmpFile = Files.createTempFile("fastcsv", ".csv");
3231
tmpFile.toFile().deleteOnExit();
3332

3433
try (var out = Files.newOutputStream(tmpFile);
35-
var csv = CsvWriter.builder().build(out, UTF_16LE)) {
34+
var csv = CsvWriter.builder().build(out, UTF_8)) {
3635

37-
// Manually write UTF-16LE BOM header
38-
out.write(new byte[]{(byte) 0xff, (byte) 0xfe});
36+
// Manually write UTF-8 BOM header
37+
out.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
3938

4039
csv.writeRecord("a", "o", "u");
4140
csv.writeRecord("ä", "ö", "ü");

0 commit comments

Comments
 (0)