|
35 | 35 |
|
36 | 36 | /** |
37 | 37 | * Specifies the format of a CSV file and parses input. |
| 38 | + * <h4>Using predefined formats</h4> |
38 | 39 | * <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: |
40 | 55 | * </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("N/A") |
| 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: |
42 | 62 | * </p> |
43 | 63 | * <pre>CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3");</pre> |
44 | 64 | * <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: |
46 | 73 | * </p> |
47 | 74 | * <pre>Reader in = ...; |
48 | 75 | *CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3").parse(in);</pre> |
49 | 76 | * <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("Col1");</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> |
51 | 97 | * @version $Id$ |
52 | 98 | */ |
53 | 99 | public class CSVFormat implements Serializable { |
|
0 commit comments