Format examples for fluent style
This commit is contained in:
parent
0bcdcfd466
commit
4ca0556f6b
|
@ -71,24 +71,23 @@ for (CSVRecord record : records) {
|
||||||
for example:
|
for example:
|
||||||
</p>
|
</p>
|
||||||
<source>final URL url = ...;
|
<source>final URL url = ...;
|
||||||
final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
|
try (final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8");
|
||||||
final CSVParser parser = CSVFormat.EXCEL.builder().setHeader().build().parse(reader);
|
final CSVParser parser = CSVFormat.EXCEL.builder()
|
||||||
try {
|
.setHeader()
|
||||||
|
.build()
|
||||||
|
.parse(reader)) {
|
||||||
for (final CSVRecord record : parser) {
|
for (final CSVRecord record : parser) {
|
||||||
final String string = record.get("SomeColumn");
|
final String string = record.get("SomeColumn");
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
parser.close();
|
|
||||||
reader.close();
|
|
||||||
}
|
}
|
||||||
</source>
|
</source>
|
||||||
<p>
|
<p>
|
||||||
You might find it handy to create something like this:
|
You might find it handy to create something like this:
|
||||||
</p>
|
</p>
|
||||||
<source>/**
|
<source>/**
|
||||||
* Creates a reader capable of handling BOMs.
|
* Creates a reader capable of handling BOMs.
|
||||||
*/
|
*/
|
||||||
public InputStreamReader newReader(final InputStream inputStream) {
|
public InputStreamReader newReader(final InputStream inputStream) {
|
||||||
return new InputStreamReader(new BOMInputStream(inputStream), StandardCharsets.UTF_8);
|
return new InputStreamReader(new BOMInputStream(inputStream), StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
@ -118,7 +117,10 @@ for (CSVRecord record : records) {
|
||||||
Indices may not be the most intuitive way to access record values. For this reason it is possible to
|
Indices may not be the most intuitive way to access record values. For this reason it is possible to
|
||||||
assign names to each column in the file:
|
assign names to each column in the file:
|
||||||
<source>Reader in = new FileReader("path/to/file.csv");
|
<source>Reader in = new FileReader("path/to/file.csv");
|
||||||
Iterable<CSVRecord> records = CSVFormat.RFC4180.builder().setHeader("ID", "CustomerNo", "Name").build().parse(in);
|
Iterable<CSVRecord> records = CSVFormat.RFC4180.builder()
|
||||||
|
.setHeader("ID", "CustomerNo", "Name")
|
||||||
|
.build()
|
||||||
|
.parse(in);
|
||||||
for (CSVRecord record : records) {
|
for (CSVRecord record : records) {
|
||||||
String id = record.get("ID");
|
String id = record.get("ID");
|
||||||
String customerNo = record.get("CustomerNo");
|
String customerNo = record.get("CustomerNo");
|
||||||
|
@ -136,7 +138,10 @@ for (CSVRecord record : records) {
|
||||||
ID, CustomerNo, Name
|
ID, CustomerNo, Name
|
||||||
}
|
}
|
||||||
Reader in = new FileReader("path/to/file.csv");
|
Reader in = new FileReader("path/to/file.csv");
|
||||||
Iterable<CSVRecord> records = CSVFormat.RFC4180.builder().setHeader(Headers.class).build().parse(in);
|
Iterable<CSVRecord> records = CSVFormat.RFC4180.builder()
|
||||||
|
.setHeader(Headers.class)
|
||||||
|
.build()
|
||||||
|
.parse(in);
|
||||||
for (CSVRecord record : records) {
|
for (CSVRecord record : records) {
|
||||||
String id = record.get(Headers.ID);
|
String id = record.get(Headers.ID);
|
||||||
String customerNo = record.get(Headers.CustomerNo);
|
String customerNo = record.get(Headers.CustomerNo);
|
||||||
|
@ -149,7 +154,11 @@ for (CSVRecord record : records) {
|
||||||
Some CSV files define header names in their first record. If configured, Apache Commons CSV can parse
|
Some CSV files define header names in their first record. If configured, Apache Commons CSV can parse
|
||||||
the header names from the first record:
|
the header names from the first record:
|
||||||
<source>Reader in = new FileReader("path/to/file.csv");
|
<source>Reader in = new FileReader("path/to/file.csv");
|
||||||
Iterable<CSVRecord> records = CSVFormat.RFC4180.builder().setHeader().setSkipHeaderRecord(true).build().parse(in);
|
Iterable<CSVRecord> records = CSVFormat.RFC4180.builder()
|
||||||
|
.setHeader()
|
||||||
|
.setSkipHeaderRecord(true)
|
||||||
|
.build()
|
||||||
|
.parse(in);
|
||||||
for (CSVRecord record : records) {
|
for (CSVRecord record : records) {
|
||||||
String id = record.get("ID");
|
String id = record.get("ID");
|
||||||
String customerNo = record.get("CustomerNo");
|
String customerNo = record.get("CustomerNo");
|
||||||
|
@ -163,16 +172,22 @@ for (CSVRecord record : records) {
|
||||||
To print a CSV file with headers, you specify the headers in the format:
|
To print a CSV file with headers, you specify the headers in the format:
|
||||||
</p>
|
</p>
|
||||||
<source>final Appendable out = ...;
|
<source>final Appendable out = ...;
|
||||||
final CSVPrinter printer = CSVFormat.DEFAULT.builder().setHeader("H1", "H2").build().print(out);
|
final CSVPrinter printer = CSVFormat.DEFAULT.builder()
|
||||||
|
.setHeader("H1", "H2")
|
||||||
|
.build()
|
||||||
|
.print(out);
|
||||||
</source>
|
</source>
|
||||||
<p>
|
<p>
|
||||||
To print a CSV file with JDBC column labels, you specify the ResultSet in the format:
|
To print a CSV file with JDBC column labels, you specify the ResultSet in the format:
|
||||||
</p>
|
</p>
|
||||||
<source>final ResultSet resultSet = ...;
|
<source>try (final ResultSet resultSet = ...) {
|
||||||
final CSVPrinter printer = CSVFormat.DEFAULT.builder().setHeader(resultSet).build().print(out);
|
final CSVPrinter printer = CSVFormat.DEFAULT.builder()
|
||||||
|
.setHeader(resultSet)
|
||||||
|
.build()
|
||||||
|
.print(out);
|
||||||
|
}
|
||||||
</source>
|
</source>
|
||||||
</subsection>
|
</subsection>
|
||||||
</section>
|
</section>
|
||||||
<!-- ================================================== -->
|
|
||||||
</body>
|
</body>
|
||||||
</document>
|
</document>
|
||||||
|
|
Loading…
Reference in New Issue