Skip to content

Commit 7e63096

Browse files
committed
Javadoc.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1509368 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7aca6ad commit 7e63096

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,65 @@
3535

3636
/**
3737
* Specifies the format of a CSV file and parses input.
38+
* <h4>Using predefined formats</h4>
3839
* <p>
39-
* This class is immutable.
40+
* You can use one of the predefined formats:
41+
* </p>
42+
* <ul>
43+
* <li>{@link #DEFAULT}</li>
44+
* <li>{@link #EXCEL}</li>
45+
* <li>{@link #MYSQL}</li>
46+
* <li>{@link #RFC4180}</li>
47+
* </ul>
48+
* <p>For example:</p>
49+
* <pre>CSVParser parser = CSVFormat.EXCEL.parse(reader);</pre>
50+
* <p>The {@link CSVRecord} provides static methods to parse other input types, for example:</p>
51+
* <pre>CSVParser parser = CSVFormat.parseFile(file, CSVFormat.EXCEL);</pre>
52+
* <h4>Defining formats</h4>
53+
* <p>
54+
* You can extend a format by calling the {@code with} methods. For example:
4055
* </p>
41-
* You can extend a format through a builder. For example, to extend the Excel format with columns header, you write:
56+
* <pre>CSVFormat.EXCEL
57+
* .withNullString(&quot;N/A&quot;)
58+
* .withIgnoreSurroundingSpaces(true);</pre>
59+
* <h4>Defining column names</h4>
60+
* <p>
61+
* To define the column names you want to use to access records, write:
4262
* </p>
4363
* <pre>CSVFormat.EXCEL.withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;);</pre>
4464
* <p>
45-
* You can parse through a format. For example, to parse an Excel file with columns header, you write:
65+
* Calling {@link #withHeader(String...)} let's you use the given names to address values in a {@link CSVRecord}, and
66+
* assumes that your CSV source does not contain a first record that also defines column names. If it does, then
67+
* you are overriding this metadata with your names and you should skip the first record by calling
68+
* {@link #withSkipHeaderRecord(boolean)} with {@code true}.
69+
* </p>
70+
* <h4>Parsing</h4>
71+
* <p>
72+
* You can use a format directly to parse a reader. For example, to parse an Excel file with columns header, write:
4673
* </p>
4774
* <pre>Reader in = ...;
4875
*CSVFormat.EXCEL.withHeader(&quot;Col1&quot;, &quot;Col2&quot;, &quot;Col3&quot;).parse(in);</pre>
4976
* <p>
50-
*
77+
* For other input types, like resources, files, and URLs, use the static methods on {@link CSVParser}.
78+
* </p>
79+
* <h4>Referencing columns safely</h4>
80+
* <p>
81+
* If your source contains a header record, you can simplify your code and safely reference columns,
82+
* by using {@link #withHeader(String...)} with no arguments:
83+
* </p>
84+
* <pre>CSVFormat.EXCEL.withHeader();</pre>
85+
* <p>
86+
* This causes the parser to read the first record and use its values as column names.
87+
* Then, call one of the {@link CSVRecord} get method that takes a String column name argument:
88+
* </p>
89+
* <pre>String value = record.get(&quot;Col1&quot;);</pre>
90+
* <p>
91+
* This makes your code impervious to changes in column order in the CSV file.
92+
* </p>
93+
* <h4>Notes</h4>
94+
* <p>
95+
* This class is immutable.
96+
* </p>
5197
* @version $Id$
5298
*/
5399
public class CSVFormat implements Serializable {

0 commit comments

Comments
 (0)