|
29 | 29 | import java.util.Arrays; |
30 | 30 |
|
31 | 31 | /** |
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> |
33 | 68 | */ |
34 | 69 | public final class CSVPrinter implements Flushable, Closeable { |
35 | 70 |
|
@@ -141,11 +176,17 @@ public void print(final Object value) throws IOException { |
141 | 176 | * Prints a comment on a new line among the delimiter separated values. |
142 | 177 | * |
143 | 178 | * <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 |
145 | 180 | * comments and a space will be inserted at the beginning of each new line in the comment. |
146 | 181 | * </p> |
147 | 182 | * |
| 183 | + * <p> |
148 | 184 | * 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> |
149 | 190 | * |
150 | 191 | * @param comment |
151 | 192 | * the comment to output |
|
0 commit comments