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

Commit 71459b6

Browse files
committed
CSV-231: Add more documentation to CSVPrinter
1 parent e5b2413 commit 71459b6

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</properties>
4040
<body>
4141
<release version="1.6" date="2018-MM-DD" description="Feature and bug fix release">
42+
<action issue="CSV-231" type="update" dev="britter">Add more documentation to CSVPrinter.</action>
4243
<action issue="CSV-217" type="add" dev="ggregory" due-to="Korolyov Alexei">Add autoFlush option for CsvPrinter. PR #24.</action>
4344
<action issue="CSV-219" type="fix" dev="ggregory" due-to="Zhang Hongda">The behavior of quote char using is not similar as Excel does when the first string contains CJK char(s).</action>
4445
<action issue="CSV-172" type="fix" dev="ggregory" due-to="Andrew Pennebaker">Don't quote cells just because they have UTF-8 encoded characters.</action>

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,42 @@
2929
import java.util.Arrays;
3030

3131
/**
32-
* Prints values in a CSV format.
32+
* Prints values in a {@link CSVFormat CSV format}.
33+
*
34+
* <p>Values can be appended to the output by calling the {@link #print(Object)} method.
35+
* Values are printed according to {@link String#valueOf(Object)}.
36+
* To complete a record the {@link #println()} method has to be called.
37+
* Comments can be appended by calling {@link #printComment(String)}.
38+
* However a comment will only be written to the output if the {@link CSVFormat} supports comments.
39+
* </p>
40+
*
41+
* <p>The printer also supports appending a complete record at once by calling {@link #printRecord(Object...)}
42+
* or {@link #printRecord(Iterable)}.
43+
* Furthermore {@link #printRecords(Object...)}, {@link #printRecords(Iterable)} and {@link #printRecords(ResultSet)}
44+
* methods can be used to print several records at once.
45+
* </p>
46+
*
47+
* <p>Example:</p>
48+
*
49+
* <pre>
50+
* try (CSVPrinter printer = new CSVPrinter(new FileWriter("csv.txt"), CSVFormat.EXCEL)) {
51+
* printer.printRecord("id", "userName", "firstName", "lastName", "birthday");
52+
* printer.printRecord(1, "john73", "John", "Doe", LocalDate.of(1973, 9, 15));
53+
* printer.println();
54+
* printer.printRecord(2, "mary", "Mary", "Meyer", LocalDate.of(1985, 3, 29));
55+
* } catch (IOException ex) {
56+
* ex.printStackTrace();
57+
* }
58+
* </pre>
59+
*
60+
* <p>This code will write the following to csv.txt:</p>
61+
* <pre>
62+
* id,userName,firstName,lastName,birthday
63+
* 1,john73,John,Doe,1973-09-15
64+
*
65+
* 2,mary,Mary,Meyer,1985-03-29
66+
* </pre>
67+
* <p></p>
3368
*/
3469
public final class CSVPrinter implements Flushable, Closeable {
3570

@@ -141,11 +176,17 @@ public void print(final Object value) throws IOException {
141176
* Prints a comment on a new line among the delimiter separated values.
142177
*
143178
* <p>
144-
* Comments will always begin on a new line and occupy a least one full line. The character specified to start
179+
* Comments will always begin on a new line and occupy at least one full line. The character specified to start
145180
* comments and a space will be inserted at the beginning of each new line in the comment.
146181
* </p>
147182
*
183+
* <p>
148184
* If comments are disabled in the current CSV format this method does nothing.
185+
* </p>
186+
*
187+
* <p>This method detects line breaks inside the comment string and inserts {@link CSVFormat#getRecordSeparator()}
188+
* to start a new line of the comment. Note that this might produce unexpected results for formats that do not use
189+
* line breaks as record separator.</p>
149190
*
150191
* @param comment
151192
* the comment to output

0 commit comments

Comments
 (0)