From 5b6cba06e49468cf669d5f1a9505658e9ac905ac Mon Sep 17 00:00:00 2001 From: samabcde Date: Mon, 1 May 2023 13:23:36 +0800 Subject: [PATCH 1/2] [CSV-306] followup to fix deprecated method in user guide --- src/site/xdoc/user-guide.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/site/xdoc/user-guide.xml b/src/site/xdoc/user-guide.xml index 4ce14e1b..6c580338 100644 --- a/src/site/xdoc/user-guide.xml +++ b/src/site/xdoc/user-guide.xml @@ -72,7 +72,7 @@ for (CSVRecord record : records) {

final URL url = ...; final Reader reader = new InputStreamReader(new BOMInputStream(url.openStream()), "UTF-8"); -final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); +final CSVParser parser = CSVFormat.EXCEL.builder().setHeader().build().parse(reader); try { for (final CSVRecord record : parser) { final String string = record.get("SomeColumn"); @@ -118,7 +118,7 @@ for (CSVRecord record : records) { 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: Reader in = new FileReader("path/to/file.csv"); -Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader("ID", "CustomerNo", "Name").parse(in); +Iterable<CSVRecord> records = CSVFormat.RFC4180.builder().setHeader("ID", "CustomerNo", "Name").build().parse(in); for (CSVRecord record : records) { String id = record.get("ID"); String customerNo = record.get("CustomerNo"); @@ -136,7 +136,7 @@ for (CSVRecord record : records) { ID, CustomerNo, Name } Reader in = new FileReader("path/to/file.csv"); -Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader(Headers.class).parse(in); +Iterable<CSVRecord> records = CSVFormat.RFC4180.builder().setHeader(Headers.class).build().parse(in); for (CSVRecord record : records) { String id = record.get(Headers.ID); String customerNo = record.get(Headers.CustomerNo); @@ -149,7 +149,7 @@ for (CSVRecord record : records) { Some CSV files define header names in their first record. If configured, Apache Commons CSV can parse the header names from the first record: Reader in = new FileReader("path/to/file.csv"); -Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader().withSkipHeaderRecord(true).parse(in); +Iterable<CSVRecord> records = CSVFormat.RFC4180.builder().setHeader().setSkipHeaderRecord(true).build().parse(in); for (CSVRecord record : records) { String id = record.get("ID"); String customerNo = record.get("CustomerNo"); @@ -163,13 +163,13 @@ for (CSVRecord record : records) { To print a CSV file with headers, you specify the headers in the format:

final Appendable out = ...; - final CSVPrinter printer = CSVFormat.DEFAULT.withHeader("H1", "H2").print(out) + final CSVPrinter printer = CSVFormat.DEFAULT.builder().setHeader("H1", "H2").build().print(out);

To print a CSV file with JDBC column labels, you specify the ResultSet in the format:

final ResultSet resultSet = ...; - final CSVPrinter printer = CSVFormat.DEFAULT.withHeader(resultSet).print(out) + final CSVPrinter printer = CSVFormat.DEFAULT.builder().setHeader(resultSet).build().print(out); From 28350056812f0269f1074d135808632bd2e9e222 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Mon, 1 May 2023 12:25:34 +0200 Subject: [PATCH 2/2] [CSV-306] Add pull request #325 to changes.xml --- src/changes/changes.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5b8bdfb5..ec8e1b71 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -56,7 +56,7 @@ Validate input to setDelimiter(String) for empty string #266. Bump CSVFormat#serialVersionUID from 1 to 2. CSVParser: Identify duplicates in null, empty and blank header names #279. - Replace deprecated method in user guide, update external link #324. + Replace deprecated method in user guide, update external link #324, #325. Serialization in CSVFormat is not supported from one version to the next.