Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 0e8811d

Browse files
committed
Document how to handle BOMs. See also [CSV-107] CSVFormat.EXCEL.parse should handle byte order marks.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1606616 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3294ed6 commit 0e8811d

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

src/site/xdoc/index.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,28 @@ for (CSVRecord record : records) {
3232
String firstName = record.get("First Name");
3333
}</source>
3434
<p>Other formats are available, please consult the Javadoc for <a href="apidocs/org/apache/commons/csv/CSVFormat.html">CSVFormat</a> and
35-
<a href="apidocs/org/apache/commons/csv/CSVParser.html">CSVParser</a>.</p>
35+
<a href="apidocs/org/apache/commons/csv/CSVParser.html">CSVParser</a>.
36+
</p>
37+
</section>
38+
<section name="Handling Byte Order Marks">
39+
<p>
40+
To handle files that start with a Byte Order Mark (BOM) like some Excel CSV files, you need an extra step to deal with these optional bytes.
41+
You can use the
42+
<a href="https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/BOMInputStream.html">BOMInputStream</a>
43+
class from <a href="https://commons.apache.org/proper/commons-io/">Apache Commons IO</a> for example:
44+
</p>
45+
<source>final URL url = ...;
46+
final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
47+
final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
48+
try {
49+
for (final CSVRecord record : parser) {
50+
final String string = record.get("SomeColumn");
51+
...
52+
}
53+
} finally {
54+
parser.close();
55+
reader.close();
56+
}</source>
3657
</section>
3758

3859
<section name="Getting the code">

0 commit comments

Comments
 (0)