Skip to content

Commit 4ca0556

Browse files
committed
Format examples for fluent style
1 parent 0bcdcfd commit 4ca0556

1 file changed

Lines changed: 30 additions & 15 deletions

File tree

src/site/xdoc/user-guide.xml

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,23 @@ for (CSVRecord record : records) {
7171
for example:
7272
</p>
7373
<source>final URL url = ...;
74-
final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
75-
final CSVParser parser = CSVFormat.EXCEL.builder().setHeader().build().parse(reader);
76-
try {
74+
try (final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
75+
final CSVParser parser = CSVFormat.EXCEL.builder()
76+
.setHeader()
77+
.build()
78+
.parse(reader)) {
7779
for (final CSVRecord record : parser) {
7880
final String string = record.get("SomeColumn");
7981
...
8082
}
81-
} finally {
82-
parser.close();
83-
reader.close();
8483
}
8584
</source>
8685
<p>
8786
You might find it handy to create something like this:
8887
</p>
8988
<source>/**
90-
* Creates a reader capable of handling BOMs.
91-
*/
89+
* Creates a reader capable of handling BOMs.
90+
*/
9291
public InputStreamReader newReader(final InputStream inputStream) {
9392
return new InputStreamReader(new BOMInputStream(inputStream), StandardCharsets.UTF_8);
9493
}
@@ -118,7 +117,10 @@ for (CSVRecord record : records) {
118117
Indices may not be the most intuitive way to access record values. For this reason it is possible to
119118
assign names to each column in the file:
120119
<source>Reader in = new FileReader(&quot;path/to/file.csv&quot;);
121-
Iterable&lt;CSVRecord&gt; records = CSVFormat.RFC4180.builder().setHeader("ID", "CustomerNo", "Name").build().parse(in);
120+
Iterable&lt;CSVRecord&gt; records = CSVFormat.RFC4180.builder()
121+
.setHeader("ID", "CustomerNo", "Name")
122+
.build()
123+
.parse(in);
122124
for (CSVRecord record : records) {
123125
String id = record.get("ID");
124126
String customerNo = record.get("CustomerNo");
@@ -136,7 +138,10 @@ for (CSVRecord record : records) {
136138
ID, CustomerNo, Name
137139
}
138140
Reader in = new FileReader(&quot;path/to/file.csv&quot;);
139-
Iterable&lt;CSVRecord&gt; records = CSVFormat.RFC4180.builder().setHeader(Headers.class).build().parse(in);
141+
Iterable&lt;CSVRecord&gt; records = CSVFormat.RFC4180.builder()
142+
.setHeader(Headers.class)
143+
.build()
144+
.parse(in);
140145
for (CSVRecord record : records) {
141146
String id = record.get(Headers.ID);
142147
String customerNo = record.get(Headers.CustomerNo);
@@ -149,7 +154,11 @@ for (CSVRecord record : records) {
149154
Some CSV files define header names in their first record. If configured, Apache Commons CSV can parse
150155
the header names from the first record:
151156
<source>Reader in = new FileReader(&quot;path/to/file.csv&quot;);
152-
Iterable&lt;CSVRecord&gt; records = CSVFormat.RFC4180.builder().setHeader().setSkipHeaderRecord(true).build().parse(in);
157+
Iterable&lt;CSVRecord&gt; records = CSVFormat.RFC4180.builder()
158+
.setHeader()
159+
.setSkipHeaderRecord(true)
160+
.build()
161+
.parse(in);
153162
for (CSVRecord record : records) {
154163
String id = record.get("ID");
155164
String customerNo = record.get("CustomerNo");
@@ -163,16 +172,22 @@ for (CSVRecord record : records) {
163172
To print a CSV file with headers, you specify the headers in the format:
164173
</p>
165174
<source>final Appendable out = ...;
166-
final CSVPrinter printer = CSVFormat.DEFAULT.builder().setHeader("H1", "H2").build().print(out);
175+
final CSVPrinter printer = CSVFormat.DEFAULT.builder()
176+
.setHeader("H1", "H2")
177+
.build()
178+
.print(out);
167179
</source>
168180
<p>
169181
To print a CSV file with JDBC column labels, you specify the ResultSet in the format:
170182
</p>
171-
<source>final ResultSet resultSet = ...;
172-
final CSVPrinter printer = CSVFormat.DEFAULT.builder().setHeader(resultSet).build().print(out);
183+
<source>try (final ResultSet resultSet = ...) {
184+
final CSVPrinter printer = CSVFormat.DEFAULT.builder()
185+
.setHeader(resultSet)
186+
.build()
187+
.print(out);
188+
}
173189
</source>
174190
</subsection>
175191
</section>
176-
<!-- ================================================== -->
177192
</body>
178193
</document>

0 commit comments

Comments
 (0)