Search code examples
csvapache-commons-csv

Get raw record in Apache CSV


How to get the raw record, i.e. the line the way it is in the csv file, as a string when using Apache CSV


Solution

  • It can be archived to closet to your requirement by using different CSV Formats. Check Below which one suits you.

    Available Formats are Following

    CSVFormat DEFAULT Description: Standard comma separated format, as per RFC4180 specification but allowing empty lines.

    CSVFormat RFC4180 Description: Comma separated format as defined by RFC4180 specification Ref: https://www.rfc-editor.org/rfc/rfc4180

    CSVFormat EXCEL Description: Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is locale dependent, it might be necessary to customize this format to accommodate to your regional settings.

    CSVFormat TDF Description: Tab-delimited format.

    CSVFormat MYSQL Description: Default MySQL format used by the SELECT INTO OUTFILE and code LOAD DATA INFILE operations. This is a tab-delimited format with a LF character as the line separator. Values are not quoted and special characters are escaped with ''. Ref: http://dev.mysql.com/doc/refman/5.1/en/load-data.html

    Also, apart from the standard CSVFormat There are many methods you can use as per your requirement.

    1. withAllowMissingColumnNames() - Sets the missing column names behavior of the format to true
    2. withAllowMissingColumnNames(boolean allowMissingColumnNames) - Sets the missing column names behavior of the format.
    3. withCommentMarker(char commentMarker) - Sets the comment start marker of the format to the specified character.
    4. withCommentMarker(Character commentMarker) -Sets the comment start marker of the format to the specified character.
    5. withDelimiter(char delimiter) - Sets the delimiter of the format to the specified character.
    6. withEscape(char escape) - Sets the escape character of the format to the specified character.
    7. withEscape(Character escape) -Sets the escape character of the format to the specified character.
    8. withHeader(ResultSet resultSet) -Sets the header of the format.
    9. withHeader(ResultSetMetaData metaData) -Sets the header of the format.
    10. withHeader(String... header) -Sets the header of the format.
    11. withHeaderComments(Object... headerComments) -Sets the header comments of the format.
    12. withIgnoreEmptyLines() -Sets the empty line skipping behavior of the format to true.
    13. withIgnoreEmptyLines(boolean ignoreEmptyLines) - Sets the empty line skipping behavior of the format.
    14. withIgnoreSurroundingSpaces() - Sets the trimming behavior of the format to true.
    15. withIgnoreSurroundingSpaces(boolean ignoreSurroundingSpaces) - Sets the trimming behavior of the format.
    16. withNullString(String nullString) - Performs conversions to and from null for strings on input and output.
    17. withQuote(char quoteChar) - Sets the quoteChar of the format to the specified character.
    18. withQuote(Character quoteChar) - Sets the quoteChar of the format to the specified character.
    19. withQuoteMode(QuoteMode quoteModePolicy) -Sets the output quote policy of the format to the specified value.
    20. withRecordSeparator(char recordSeparator) - Sets the record separator of the format to the specified character.
    21. withRecordSeparator(String recordSeparator) - Sets the record separator of the format to the specified String.