Package org.apache.commons.csv

Examples of org.apache.commons.csv.CSVFormat


    private BufferedReader getBufferedReader() throws IOException {
        return new BufferedReader(new FileReader(BIG_FILE));
    }

    private long parse(final Reader in, final boolean traverseColumns) throws IOException {
        final CSVFormat format = CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false);
        long recordCount = 0;
        for (final CSVRecord record : format.parse(in)) {
            recordCount++;
            if (traverseColumns) {
                for (@SuppressWarnings("unused") final String value : record) {
                    // do nothing for now
                }
View Full Code Here


     * ignore empty lines allows the last data line to have a recordSeparator
     *
     * @return CSVFormat based on constructor settings.
     */
    private CSVFormat buildFormat() {
        CSVFormat format = CSVFormat.DEFAULT
                .withIgnoreEmptyLines(true)
                .withDelimiter(asControlCharacter(fieldDelimiter))
                .withQuoteChar(asControlCharacter(quoteCharacter));

        if (escapeCharacter != null) {
            format = format.withEscape(asControlCharacter(escapeCharacter));
        }

        switch(headerSource) {
        case FROM_TABLE:
            // obtain headers from table, so format should not expect a header.
            break;
        case IN_LINE:
            // an empty string array triggers csv loader to grab the first line as the header
            format = format.withHeader(new String[0]);
            break;
        case SUPPLIED_BY_USER:
            // a populated string array supplied by the user
            format = format.withHeader(columns.toArray(new String[columns.size()]));
            break;
        default:
            throw new RuntimeException("Header source was unable to be inferred.");

        }
View Full Code Here

     * ignore empty lines allows the last data line to have a recordSeparator
     *
     * @return CSVFormat based on constructor settings.
     */
    private CSVFormat buildFormat() {
        CSVFormat format = CSVFormat.DEFAULT
                .withIgnoreEmptyLines(true);
        if (userSuppliedMetaCharacters) {
            // list error checking handled in constructor above.
            // use 0 to keep default setting
            String delimiter = customMetaCharacters.get(0);
            String quote = customMetaCharacters.get(1);
            String escape = customMetaCharacters.get(2);

            if (!"0".equals(delimiter)) {
                format = format.withDelimiter(getCustomMetaCharacter(delimiter));
            }
            if (!"0".equals(quote)) {
                format = format.withQuoteChar(getCustomMetaCharacter(quote));
            }
            if (!"0".equals(quote)) {
                format = format.withEscape(getCustomMetaCharacter(escape));
            }

        }
        switch(headerSource) {
        case FROM_TABLE:
            // obtain headers from table, so format should not expect a header.
            break;
        case IN_LINE:
            // an empty string array triggers csv loader to grab the first line as the header
            format = format.withHeader(new String[0]);
            break;
        case SUPPLIED_BY_USER:
            // a populated string array supplied by the user
            format = format.withHeader(columns.toArray(new String[columns.size()]));
            break;
        default:
            throw new RuntimeException("Header source was unable to be inferred.");

        }
View Full Code Here

TOP

Related Classes of org.apache.commons.csv.CSVFormat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.