- java.lang.Object
-
- org.apache.commons.csv.CSVFormat
-
- All Implemented Interfaces:
java.io.Serializable
public final class CSVFormat extends java.lang.Object implements java.io.SerializableSpecifies the format of a CSV file and parses input.Using predefined formats
You can use one of the predefined formats:
DEFAULTEXCELINFORMIX_UNLOADINFORMIX_UNLOAD_CSVMYSQLRFC4180ORACLEPOSTGRESQL_CSVPOSTGRESQL_TEXTTDF
For example:
CSVParser parser = CSVFormat.EXCEL.parse(reader);
The
CSVParserprovides static methods to parse other input types, for example:CSVParser parser = CSVParser.parse(file, StandardCharsets.US_ASCII, CSVFormat.EXCEL);
Defining formats
You can extend a format by calling the
withmethods. For example:CSVFormat.EXCEL.withNullString("N/A").withIgnoreSurroundingSpaces(true);Defining column names
To define the column names you want to use to access records, write:
CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3");Calling
withHeader(String...)let's you use the given names to address values in aCSVRecord, and assumes that your CSV source does not contain a first record that also defines column names. If it does, then you are overriding this metadata with your names and you should skip the first record by callingwithSkipHeaderRecord(boolean)withtrue.Parsing
You can use a format directly to parse a reader. For example, to parse an Excel file with columns header, write:
Reader in = ...; CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3").parse(in);For other input types, like resources, files, and URLs, use the static methods on
CSVParser.Referencing columns safely
If your source contains a header record, you can simplify your code and safely reference columns, by using
withHeader(String...)with no arguments:CSVFormat.EXCEL.withHeader();
This causes the parser to read the first record and use its values as column names. Then, call one of the
CSVRecordget method that takes a String column name argument:String value = record.get("Col1");This makes your code impervious to changes in column order in the CSV file.
Notes
This class is immutable.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCSVFormat.PredefinedPredefines formats.
-
Field Summary
Fields Modifier and Type Field Description static CSVFormatDEFAULTStandard Comma Separated Value format, as forRFC4180but allowing empty lines.static CSVFormatEXCELExcel file format (using a comma as the value delimiter).static CSVFormatINFORMIX_UNLOADDefault Informix CSV UNLOAD format used by theUNLOAD TO file_nameoperation.static CSVFormatINFORMIX_UNLOAD_CSVDefault Informix CSV UNLOAD format used by theUNLOAD TO file_nameoperation (escaping is disabled.)static CSVFormatMONGODB_CSVDefault MongoDB CSV format used by themongoexportoperation.static CSVFormatMONGODB_TSVDefault MongoDB TSV format used by themongoexportoperation.static CSVFormatMYSQLDefault MySQL format used by theSELECT INTO OUTFILEandLOAD DATA INFILEoperations.static CSVFormatORACLEDefault Oracle format used by the SQL*Loader utility.static CSVFormatPOSTGRESQL_CSVDefault PostgreSQL CSV format used by theCOPYoperation.static CSVFormatPOSTGRESQL_TEXTDefault PostgreSQL text format used by theCOPYoperation.static CSVFormatRFC4180Comma separated format as defined by RFC 4180.static CSVFormatTDFTab-delimited format.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)java.lang.Stringformat(java.lang.Object... values)Formats the specified values.booleangetAllowMissingColumnNames()Specifies whether missing column names are allowed when parsing the header line.booleangetAutoFlush()Returns whether to flush on close.java.lang.CharactergetCommentMarker()Returns the character marking the start of a line comment.chargetDelimiter()Returns the character delimiting the values (typically ';', ',' or '\t').java.lang.CharactergetEscapeCharacter()Returns the escape character.java.lang.String[]getHeader()Returns a copy of the header array.java.lang.String[]getHeaderComments()Returns a copy of the header comment array.booleangetIgnoreEmptyLines()Specifies whether empty lines between records are ignored when parsing input.booleangetIgnoreHeaderCase()Specifies whether header names will be accessed ignoring case.booleangetIgnoreSurroundingSpaces()Specifies whether spaces around values are ignored when parsing input.java.lang.StringgetNullString()Gets the String to convert to and fromnull.java.lang.CharactergetQuoteCharacter()Returns the character used to encapsulate values containing special characters.QuoteModegetQuoteMode()Returns the quote policy output fields.java.lang.StringgetRecordSeparator()Returns the record separator delimiting output records.booleangetSkipHeaderRecord()Returns whether to skip the header record.booleangetTrailingDelimiter()Returns whether to add a trailing delimiter.booleangetTrim()Returns whether to trim leading and trailing blanks.inthashCode()booleanisCommentMarkerSet()Specifies whether comments are supported by this format.booleanisEscapeCharacterSet()Returns whether escape are being processed.booleanisNullStringSet()Returns whether a nullString has been defined.booleanisQuoteCharacterSet()Returns whether a quoteChar has been defined.static CSVFormatnewFormat(char delimiter)Creates a new CSV format with the specified delimiter.CSVParserparse(java.io.Reader in)Parses the specified content.CSVPrinterprint(java.io.File out, java.nio.charset.Charset charset)Prints to the specified output.CSVPrinterprint(java.lang.Appendable out)Prints to the specified output.voidprint(java.lang.Object value, java.lang.Appendable out, boolean newRecord)Prints thevalueas the next value on the line toout.CSVPrinterprint(java.nio.file.Path out, java.nio.charset.Charset charset)Prints to the specified output.CSVPrinterprinter()Prints to theSystem.out.voidprintln(java.lang.Appendable out)Outputs the trailing delimiter (if set) followed by the record separator (if set).voidprintRecord(java.lang.Appendable out, java.lang.Object... values)Prints the givenvaluestooutas a single record of delimiter separated values followed by the record separator.java.lang.StringtoString()static CSVFormatvalueOf(java.lang.String format)Gets one of the predefined formats fromCSVFormat.Predefined.CSVFormatwithAllowMissingColumnNames()Returns a newCSVFormatwith the missing column names behavior of the format set totrueCSVFormatwithAllowMissingColumnNames(boolean allowMissingColumnNames)Returns a newCSVFormatwith the missing column names behavior of the format set to the given value.CSVFormatwithAutoFlush(boolean autoFlush)Returns a newCSVFormatwith whether to flush on close.CSVFormatwithCommentMarker(char commentMarker)Returns a newCSVFormatwith the comment start marker of the format set to the specified character.CSVFormatwithCommentMarker(java.lang.Character commentMarker)Returns a newCSVFormatwith the comment start marker of the format set to the specified character.CSVFormatwithDelimiter(char delimiter)Returns a newCSVFormatwith the delimiter of the format set to the specified character.CSVFormatwithEscape(char escape)Returns a newCSVFormatwith the escape character of the format set to the specified character.CSVFormatwithEscape(java.lang.Character escape)Returns a newCSVFormatwith the escape character of the format set to the specified character.CSVFormatwithFirstRecordAsHeader()Returns a newCSVFormatusing the first record as header.CSVFormatwithHeader(java.lang.Class<? extends java.lang.Enum<?>> headerEnum)Returns a newCSVFormatwith the header of the format defined by the enum class.CSVFormatwithHeader(java.lang.String... header)Returns a newCSVFormatwith the header of the format set to the given values.CSVFormatwithHeaderComments(java.lang.Object... headerComments)Returns a newCSVFormatwith the header comments of the format set to the given values.CSVFormatwithIgnoreEmptyLines()Returns a newCSVFormatwith the empty line skipping behavior of the format set totrue.CSVFormatwithIgnoreEmptyLines(boolean ignoreEmptyLines)Returns a newCSVFormatwith the empty line skipping behavior of the format set to the given value.CSVFormatwithIgnoreHeaderCase()Returns a newCSVFormatwith the header ignore case behavior set totrue.CSVFormatwithIgnoreHeaderCase(boolean ignoreHeaderCase)Returns a newCSVFormatwith whether header names should be accessed ignoring case.CSVFormatwithIgnoreSurroundingSpaces()Returns a newCSVFormatwith the trimming behavior of the format set totrue.CSVFormatwithIgnoreSurroundingSpaces(boolean ignoreSurroundingSpaces)Returns a newCSVFormatwith the trimming behavior of the format set to the given value.CSVFormatwithNullString(java.lang.String nullString)Returns a newCSVFormatwith conversions to and from null for strings on input and output.CSVFormatwithQuote(char quoteChar)Returns a newCSVFormatwith the quoteChar of the format set to the specified character.CSVFormatwithQuote(java.lang.Character quoteChar)Returns a newCSVFormatwith the quoteChar of the format set to the specified character.CSVFormatwithQuoteMode(QuoteMode quoteModePolicy)Returns a newCSVFormatwith the output quote policy of the format set to the specified value.CSVFormatwithRecordSeparator(char recordSeparator)Returns a newCSVFormatwith the record separator of the format set to the specified character.CSVFormatwithRecordSeparator(java.lang.String recordSeparator)Returns a newCSVFormatwith the record separator of the format set to the specified String.CSVFormatwithSkipHeaderRecord()Returns a newCSVFormatwith skipping the header record set totrue.CSVFormatwithSkipHeaderRecord(boolean skipHeaderRecord)Returns a newCSVFormatwith whether to skip the header record.CSVFormatwithSystemRecordSeparator()Returns a newCSVFormatwith the record separator of the format set to the operating system's line separator string, typically CR+LF on Windows and LF on Linux.CSVFormatwithTrailingDelimiter()Returns a newCSVFormatto add a trailing delimiter.CSVFormatwithTrailingDelimiter(boolean trailingDelimiter)Returns a newCSVFormatwith whether to add a trailing delimiter.CSVFormatwithTrim()Returns a newCSVFormatto trim leading and trailing blanks.CSVFormatwithTrim(boolean trim)Returns a newCSVFormatwith whether to trim leading and trailing blanks.
-
-
-
Field Detail
-
DEFAULT
public static final CSVFormat DEFAULT
Standard Comma Separated Value format, as forRFC4180but allowing empty lines.Settings are:
withDelimiter(',')withQuote('"')withRecordSeparator("\r\n")withIgnoreEmptyLines(true)
- See Also:
CSVFormat.Predefined.Default
-
EXCEL
public static final CSVFormat EXCEL
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.For example for parsing or generating a CSV file on a French system the following format will be used:
CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');Settings are:
{@link #withDelimiter(char) withDelimiter(',')}{@link #withQuote(char) withQuote('"')}{@link #withRecordSeparator(String) withRecordSeparator("\r\n")}{@link #withIgnoreEmptyLines(boolean) withIgnoreEmptyLines(false)}{@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}
Note: This is currently like
RFC4180pluswithAllowMissingColumnNames(true)andwithIgnoreEmptyLines(false).- See Also:
CSVFormat.Predefined.Excel
-
INFORMIX_UNLOAD
public static final CSVFormat INFORMIX_UNLOAD
Default Informix CSV UNLOAD format used by theUNLOAD TO file_nameoperation.This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special characters are escaped with
'\'. The default NULL string is"\\N".Settings are:
withDelimiter(',')withEscape('\\')withQuote("\"")withRecordSeparator('\n')
-
INFORMIX_UNLOAD_CSV
public static final CSVFormat INFORMIX_UNLOAD_CSV
Default Informix CSV UNLOAD format used by theUNLOAD TO file_nameoperation (escaping is disabled.)This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special characters are escaped with
'\'. The default NULL string is"\\N".Settings are:
withDelimiter(',')withQuote("\"")withRecordSeparator('\n')
-
MONGODB_CSV
public static final CSVFormat MONGODB_CSV
Default MongoDB CSV format used by themongoexportoperation.Parsing is not supported yet.
This is a comma-delimited format. Values are double quoted only if needed and special characters are escaped with
'"'. A header line with field names is expected.Settings are:
withDelimiter(',')withEscape('"')withQuote('"')withQuoteMode(QuoteMode.ALL_NON_NULL)withSkipHeaderRecord(false)
- Since:
- 1.7
- See Also:
CSVFormat.Predefined.MongoDBCsv, MongoDB mongoexport command documentation
-
MONGODB_TSV
public static final CSVFormat MONGODB_TSV
Default MongoDB TSV format used by themongoexportoperation.Parsing is not supported yet.
This is a tab-delimited format. Values are double quoted only if needed and special characters are escaped with
'"'. A header line with field names is expected.Settings are:
withDelimiter('\t')withEscape('"')withQuote('"')withQuoteMode(QuoteMode.ALL_NON_NULL)withSkipHeaderRecord(false)
- Since:
- 1.7
- See Also:
CSVFormat.Predefined.MongoDBCsv, MongoDB mongoexport command documentation
-
MYSQL
public static final CSVFormat MYSQL
Default MySQL format used by theSELECT INTO OUTFILEandLOAD DATA INFILEoperations.This is a tab-delimited format with a LF character as the line separator. Values are not quoted and special characters are escaped with
'\'. The default NULL string is"\\N".Settings are:
withDelimiter('\t')withEscape('\\')withIgnoreEmptyLines(false)withQuote(null)withRecordSeparator('\n')withNullString("\\N")withQuoteMode(QuoteMode.ALL_NON_NULL)
-
ORACLE
public static final CSVFormat ORACLE
Default Oracle format used by the SQL*Loader utility.This is a comma-delimited format with the system line separator character as the record separator.Values are double quoted when needed and special characters are escaped with
'"'. The default NULL string is"". Values are trimmed.Settings are:
withDelimiter(',') // default is {@code FIELDS TERMINATED BY ','}withEscape('\\')withIgnoreEmptyLines(false)withQuote('"') // default is {@code OPTIONALLY ENCLOSED BY '"'}withNullString("\\N")withTrim()withSystemRecordSeparator()withQuoteMode(QuoteMode.MINIMAL)
- Since:
- 1.6
- See Also:
CSVFormat.Predefined.Oracle, Oracle CSV Format Specification
-
POSTGRESQL_CSV
public static final CSVFormat POSTGRESQL_CSV
Default PostgreSQL CSV format used by theCOPYoperation.This is a comma-delimited format with a LF character as the line separator. Values are double quoted and special characters are escaped with
'"'. The default NULL string is"".Settings are:
withDelimiter(',')withEscape('"')withIgnoreEmptyLines(false)withQuote('"')withRecordSeparator('\n')withNullString("")withQuoteMode(QuoteMode.ALL_NON_NULL)
- Since:
- 1.5
- See Also:
CSVFormat.Predefined.MySQL, PostgreSQL COPY command documentation
-
POSTGRESQL_TEXT
public static final CSVFormat POSTGRESQL_TEXT
Default PostgreSQL text format used by theCOPYoperation.This is a tab-delimited format with a LF character as the line separator. Values are double quoted and special characters are escaped with
'"'. The default NULL string is"\\N".Settings are:
withDelimiter('\t')withEscape('\\')withIgnoreEmptyLines(false)withQuote('"')withRecordSeparator('\n')withNullString("\\N")withQuoteMode(QuoteMode.ALL_NON_NULL)
- Since:
- 1.5
- See Also:
CSVFormat.Predefined.MySQL, PostgreSQL COPY command documentation
-
RFC4180
public static final CSVFormat RFC4180
Comma separated format as defined by RFC 4180.Settings are:
withDelimiter(',')withQuote('"')withRecordSeparator("\r\n")withIgnoreEmptyLines(false)
- See Also:
CSVFormat.Predefined.RFC4180
-
TDF
public static final CSVFormat TDF
Tab-delimited format.Settings are:
withDelimiter('\t')withQuote('"')withRecordSeparator("\r\n")withIgnoreSurroundingSpaces(true)
- See Also:
CSVFormat.Predefined.TDF
-
-
Method Detail
-
newFormat
public static CSVFormat newFormat(char delimiter)
Creates a new CSV format with the specified delimiter.Use this method if you want to create a CSVFormat from scratch. All fields but the delimiter will be initialized with null/false.
-
valueOf
public static CSVFormat valueOf(java.lang.String format)
Gets one of the predefined formats fromCSVFormat.Predefined.- Parameters:
format- name- Returns:
- one of the predefined formats
- Since:
- 1.2
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
format
public java.lang.String format(java.lang.Object... values)
Formats the specified values.- Parameters:
values- the values to format- Returns:
- the formatted values
-
getAllowMissingColumnNames
public boolean getAllowMissingColumnNames()
Specifies whether missing column names are allowed when parsing the header line.- Returns:
trueif missing column names are allowed when parsing the header line,falseto throw anIllegalArgumentException.
-
getAutoFlush
public boolean getAutoFlush()
Returns whether to flush on close.- Returns:
- whether to flush on close.
- Since:
- 1.6
-
getCommentMarker
public java.lang.Character getCommentMarker()
Returns the character marking the start of a line comment.- Returns:
- the comment start marker, may be
null
-
getDelimiter
public char getDelimiter()
Returns the character delimiting the values (typically ';', ',' or '\t').- Returns:
- the delimiter character
-
getEscapeCharacter
public java.lang.Character getEscapeCharacter()
Returns the escape character.- Returns:
- the escape character, may be
null
-
getHeader
public java.lang.String[] getHeader()
Returns a copy of the header array.- Returns:
- a copy of the header array;
nullif disabled, the empty array if to be read from the file
-
getHeaderComments
public java.lang.String[] getHeaderComments()
Returns a copy of the header comment array.- Returns:
- a copy of the header comment array;
nullif disabled.
-
getIgnoreEmptyLines
public boolean getIgnoreEmptyLines()
Specifies whether empty lines between records are ignored when parsing input.- Returns:
trueif empty lines between records are ignored,falseif they are turned into empty records.
-
getIgnoreHeaderCase
public boolean getIgnoreHeaderCase()
Specifies whether header names will be accessed ignoring case.- Returns:
trueif header names cases are ignored,falseif they are case sensitive.- Since:
- 1.3
-
getIgnoreSurroundingSpaces
public boolean getIgnoreSurroundingSpaces()
Specifies whether spaces around values are ignored when parsing input.- Returns:
trueif spaces around values are ignored,falseif they are treated as part of the value.
-
getNullString
public java.lang.String getNullString()
Gets the String to convert to and fromnull.- Reading: Converts strings equal to the given
nullStringtonullwhen reading records. - Writing: Writes
nullas the givennullStringwhen writing records.
- Returns:
- the String to convert to and from
null. No substitution occurs ifnull
- Reading: Converts strings equal to the given
-
getQuoteCharacter
public java.lang.Character getQuoteCharacter()
Returns the character used to encapsulate values containing special characters.- Returns:
- the quoteChar character, may be
null
-
getQuoteMode
public QuoteMode getQuoteMode()
Returns the quote policy output fields.- Returns:
- the quote policy
-
getRecordSeparator
public java.lang.String getRecordSeparator()
Returns the record separator delimiting output records.- Returns:
- the record separator
-
getSkipHeaderRecord
public boolean getSkipHeaderRecord()
Returns whether to skip the header record.- Returns:
- whether to skip the header record.
-
getTrailingDelimiter
public boolean getTrailingDelimiter()
Returns whether to add a trailing delimiter.- Returns:
- whether to add a trailing delimiter.
- Since:
- 1.3
-
getTrim
public boolean getTrim()
Returns whether to trim leading and trailing blanks.- Returns:
- whether to trim leading and trailing blanks.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
isCommentMarkerSet
public boolean isCommentMarkerSet()
Specifies whether comments are supported by this format. Note that the comment introducer character is only recognized at the start of a line.- Returns:
trueis comments are supported,falseotherwise
-
isEscapeCharacterSet
public boolean isEscapeCharacterSet()
Returns whether escape are being processed.- Returns:
trueif escapes are processed
-
isNullStringSet
public boolean isNullStringSet()
Returns whether a nullString has been defined.- Returns:
trueif a nullString is defined
-
isQuoteCharacterSet
public boolean isQuoteCharacterSet()
Returns whether a quoteChar has been defined.- Returns:
trueif a quoteChar is defined
-
parse
public CSVParser parse(java.io.Reader in) throws java.io.IOException
Parses the specified content.See also the various static parse methods on
CSVParser.- Parameters:
in- the input stream- Returns:
- a parser over a stream of
CSVRecords. - Throws:
java.io.IOException- If an I/O error occurs
-
print
public CSVPrinter print(java.lang.Appendable out) throws java.io.IOException
Prints to the specified output.See also
CSVPrinter.- Parameters:
out- the output.- Returns:
- a printer to an output.
- Throws:
java.io.IOException- thrown if the optional header cannot be printed.
-
print
public CSVPrinter print(java.io.File out, java.nio.charset.Charset charset) throws java.io.IOException
Prints to the specified output.See also
CSVPrinter.- Parameters:
out- the output.charset- A charset.- Returns:
- a printer to an output.
- Throws:
java.io.IOException- thrown if the optional header cannot be printed.- Since:
- 1.5
-
print
public void print(java.lang.Object value, java.lang.Appendable out, boolean newRecord) throws java.io.IOExceptionPrints thevalueas the next value on the line toout. The value will be escaped or encapsulated as needed. Useful when one wants to avoid creating CSVPrinters.- Parameters:
value- value to output.out- where to print the value.newRecord- if this a new record.- Throws:
java.io.IOException- If an I/O error occurs.- Since:
- 1.4
-
print
public CSVPrinter print(java.nio.file.Path out, java.nio.charset.Charset charset) throws java.io.IOException
Prints to the specified output.See also
CSVPrinter.- Parameters:
out- the output.charset- A charset.- Returns:
- a printer to an output.
- Throws:
java.io.IOException- thrown if the optional header cannot be printed.- Since:
- 1.5
-
printer
public CSVPrinter printer() throws java.io.IOException
Prints to theSystem.out.See also
CSVPrinter.- Returns:
- a printer to
System.out. - Throws:
java.io.IOException- thrown if the optional header cannot be printed.- Since:
- 1.5
-
println
public void println(java.lang.Appendable out) throws java.io.IOExceptionOutputs the trailing delimiter (if set) followed by the record separator (if set).- Parameters:
out- where to write- Throws:
java.io.IOException- If an I/O error occurs- Since:
- 1.4
-
printRecord
public void printRecord(java.lang.Appendable out, java.lang.Object... values) throws java.io.IOExceptionPrints the givenvaluestooutas a single record of delimiter separated values followed by the record separator.The values will be quoted if needed. Quotes and new-line characters will be escaped. This method adds the record separator to the output after printing the record, so there is no need to call
println(Appendable).- Parameters:
out- where to write.values- values to output.- Throws:
java.io.IOException- If an I/O error occurs.- Since:
- 1.4
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
withAllowMissingColumnNames
public CSVFormat withAllowMissingColumnNames()
Returns a newCSVFormatwith the missing column names behavior of the format set totrue- Returns:
- A new CSVFormat that is equal to this but with the specified missing column names behavior.
- Since:
- 1.1
- See Also:
withAllowMissingColumnNames(boolean)
-
withAllowMissingColumnNames
public CSVFormat withAllowMissingColumnNames(boolean allowMissingColumnNames)
Returns a newCSVFormatwith the missing column names behavior of the format set to the given value.- Parameters:
allowMissingColumnNames- the missing column names behavior,trueto allow missing column names in the header line,falseto cause anIllegalArgumentExceptionto be thrown.- Returns:
- A new CSVFormat that is equal to this but with the specified missing column names behavior.
-
withAutoFlush
public CSVFormat withAutoFlush(boolean autoFlush)
Returns a newCSVFormatwith whether to flush on close.- Parameters:
autoFlush- whether to flush on close.- Returns:
- A new CSVFormat that is equal to this but with the specified autoFlush setting.
- Since:
- 1.6
-
withCommentMarker
public CSVFormat withCommentMarker(char commentMarker)
Returns a newCSVFormatwith the comment start marker of the format set to the specified character. Note that the comment start character is only recognized at the start of a line.- Parameters:
commentMarker- the comment start marker- Returns:
- A new CSVFormat that is equal to this one but with the specified character as the comment start marker
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
withCommentMarker
public CSVFormat withCommentMarker(java.lang.Character commentMarker)
Returns a newCSVFormatwith the comment start marker of the format set to the specified character. Note that the comment start character is only recognized at the start of a line.- Parameters:
commentMarker- the comment start marker, usenullto disable- Returns:
- A new CSVFormat that is equal to this one but with the specified character as the comment start marker
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
withDelimiter
public CSVFormat withDelimiter(char delimiter)
Returns a newCSVFormatwith the delimiter of the format set to the specified character.- Parameters:
delimiter- the delimiter character- Returns:
- A new CSVFormat that is equal to this with the specified character as delimiter
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
withEscape
public CSVFormat withEscape(char escape)
Returns a newCSVFormatwith the escape character of the format set to the specified character.- Parameters:
escape- the escape character- Returns:
- A new CSVFormat that is equal to his but with the specified character as the escape character
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
withEscape
public CSVFormat withEscape(java.lang.Character escape)
Returns a newCSVFormatwith the escape character of the format set to the specified character.- Parameters:
escape- the escape character, usenullto disable- Returns:
- A new CSVFormat that is equal to this but with the specified character as the escape character
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
withFirstRecordAsHeader
public CSVFormat withFirstRecordAsHeader()
Returns a newCSVFormatusing the first record as header.Calling this method is equivalent to calling:
CSVFormat format = aFormat.withHeader().withSkipHeaderRecord();
- Returns:
- A new CSVFormat that is equal to this but using the first record as header.
- Since:
- 1.3
- See Also:
withSkipHeaderRecord(boolean),withHeader(String...)
-
withHeader
public CSVFormat withHeader(java.lang.Class<? extends java.lang.Enum<?>> headerEnum)
Returns a newCSVFormatwith the header of the format defined by the enum class.Example:
public enum Header { Name, Email, Phone } CSVFormat format = aformat.withHeader(Header.class);The header is also used by the
CSVPrinter.- Parameters:
headerEnum- the enum defining the header,nullif disabled, empty if parsed automatically, user specified otherwise.- Returns:
- A new CSVFormat that is equal to this but with the specified header
- Since:
- 1.3
- See Also:
withHeader(String...),withSkipHeaderRecord(boolean)
-
withHeader
public CSVFormat withHeader(java.lang.String... header)
Returns a newCSVFormatwith the header of the format set to the given values. The header can either be parsed automatically from the input file with:CSVFormat format = aformat.withHeader();
or specified manually with:CSVFormat format = aformat.withHeader("name", "email", "phone");The header is also used by the
CSVPrinter.- Parameters:
header- the header,nullif disabled, empty if parsed automatically, user specified otherwise.- Returns:
- A new CSVFormat that is equal to this but with the specified header
- See Also:
withSkipHeaderRecord(boolean)
-
withHeaderComments
public CSVFormat withHeaderComments(java.lang.Object... headerComments)
Returns a newCSVFormatwith the header comments of the format set to the given values. The comments will be printed first, before the headers. This setting is ignored by the parser.CSVFormat format = aformat.withHeaderComments("Generated by Apache Commons CSV 1.1.", new Date());- Parameters:
headerComments- the headerComments which will be printed by the Printer before the actual CSV data.- Returns:
- A new CSVFormat that is equal to this but with the specified header
- Since:
- 1.1
- See Also:
withSkipHeaderRecord(boolean)
-
withIgnoreEmptyLines
public CSVFormat withIgnoreEmptyLines()
Returns a newCSVFormatwith the empty line skipping behavior of the format set totrue.- Returns:
- A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
- Since:
withIgnoreEmptyLines(boolean), 1.1
-
withIgnoreEmptyLines
public CSVFormat withIgnoreEmptyLines(boolean ignoreEmptyLines)
Returns a newCSVFormatwith the empty line skipping behavior of the format set to the given value.- Parameters:
ignoreEmptyLines- the empty line skipping behavior,trueto ignore the empty lines between the records,falseto translate empty lines to empty records.- Returns:
- A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
-
withIgnoreHeaderCase
public CSVFormat withIgnoreHeaderCase()
Returns a newCSVFormatwith the header ignore case behavior set totrue.- Returns:
- A new CSVFormat that will ignore case header name.
- Since:
- 1.3
- See Also:
withIgnoreHeaderCase(boolean)
-
withIgnoreHeaderCase
public CSVFormat withIgnoreHeaderCase(boolean ignoreHeaderCase)
Returns a newCSVFormatwith whether header names should be accessed ignoring case.- Parameters:
ignoreHeaderCase- the case mapping behavior,trueto access name/values,falseto leave the mapping as is.- Returns:
- A new CSVFormat that will ignore case header name if specified as
true - Since:
- 1.3
-
withIgnoreSurroundingSpaces
public CSVFormat withIgnoreSurroundingSpaces()
Returns a newCSVFormatwith the trimming behavior of the format set totrue.- Returns:
- A new CSVFormat that is equal to this but with the specified trimming behavior.
- Since:
- 1.1
- See Also:
withIgnoreSurroundingSpaces(boolean)
-
withIgnoreSurroundingSpaces
public CSVFormat withIgnoreSurroundingSpaces(boolean ignoreSurroundingSpaces)
Returns a newCSVFormatwith the trimming behavior of the format set to the given value.- Parameters:
ignoreSurroundingSpaces- the trimming behavior,trueto remove the surrounding spaces,falseto leave the spaces as is.- Returns:
- A new CSVFormat that is equal to this but with the specified trimming behavior.
-
withNullString
public CSVFormat withNullString(java.lang.String nullString)
Returns a newCSVFormatwith conversions to and from null for strings on input and output.- Reading: Converts strings equal to the given
nullStringtonullwhen reading records. - Writing: Writes
nullas the givennullStringwhen writing records.
- Parameters:
nullString- the String to convert to and fromnull. No substitution occurs ifnull- Returns:
- A new CSVFormat that is equal to this but with the specified null conversion string.
- Reading: Converts strings equal to the given
-
withQuote
public CSVFormat withQuote(char quoteChar)
Returns a newCSVFormatwith the quoteChar of the format set to the specified character.- Parameters:
quoteChar- the quoteChar character- Returns:
- A new CSVFormat that is equal to this but with the specified character as quoteChar
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
withQuote
public CSVFormat withQuote(java.lang.Character quoteChar)
Returns a newCSVFormatwith the quoteChar of the format set to the specified character.- Parameters:
quoteChar- the quoteChar character, usenullto disable- Returns:
- A new CSVFormat that is equal to this but with the specified character as quoteChar
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
withQuoteMode
public CSVFormat withQuoteMode(QuoteMode quoteModePolicy)
Returns a newCSVFormatwith the output quote policy of the format set to the specified value.- Parameters:
quoteModePolicy- the quote policy to use for output.- Returns:
- A new CSVFormat that is equal to this but with the specified quote policy
-
withRecordSeparator
public CSVFormat withRecordSeparator(char recordSeparator)
Returns a newCSVFormatwith the record separator of the format set to the specified character.Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and "\r\n"
- Parameters:
recordSeparator- the record separator to use for output.- Returns:
- A new CSVFormat that is equal to this but with the specified output record separator
-
withRecordSeparator
public CSVFormat withRecordSeparator(java.lang.String recordSeparator)
Returns a newCSVFormatwith the record separator of the format set to the specified String.Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and "\r\n"
- Parameters:
recordSeparator- the record separator to use for output.- Returns:
- A new CSVFormat that is equal to this but with the specified output record separator
- Throws:
java.lang.IllegalArgumentException- if recordSeparator is none of CR, LF or CRLF
-
withSkipHeaderRecord
public CSVFormat withSkipHeaderRecord()
Returns a newCSVFormatwith skipping the header record set totrue.- Returns:
- A new CSVFormat that is equal to this but with the specified skipHeaderRecord setting.
- Since:
- 1.1
- See Also:
withSkipHeaderRecord(boolean),withHeader(String...)
-
withSkipHeaderRecord
public CSVFormat withSkipHeaderRecord(boolean skipHeaderRecord)
Returns a newCSVFormatwith whether to skip the header record.- Parameters:
skipHeaderRecord- whether to skip the header record.- Returns:
- A new CSVFormat that is equal to this but with the specified skipHeaderRecord setting.
- See Also:
withHeader(String...)
-
withSystemRecordSeparator
public CSVFormat withSystemRecordSeparator()
Returns a newCSVFormatwith the record separator of the format set to the operating system's line separator string, typically CR+LF on Windows and LF on Linux.Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and "\r\n"
- Returns:
- A new CSVFormat that is equal to this but with the operating system's line separator string.
- Since:
- 1.6
-
withTrailingDelimiter
public CSVFormat withTrailingDelimiter()
Returns a newCSVFormatto add a trailing delimiter.- Returns:
- A new CSVFormat that is equal to this but with the trailing delimiter setting.
- Since:
- 1.3
-
withTrailingDelimiter
public CSVFormat withTrailingDelimiter(boolean trailingDelimiter)
Returns a newCSVFormatwith whether to add a trailing delimiter.- Parameters:
trailingDelimiter- whether to add a trailing delimiter.- Returns:
- A new CSVFormat that is equal to this but with the specified trailing delimiter setting.
- Since:
- 1.3
-
withTrim
public CSVFormat withTrim()
Returns a newCSVFormatto trim leading and trailing blanks.- Returns:
- A new CSVFormat that is equal to this but with the trim setting on.
- Since:
- 1.3
-
withTrim
public CSVFormat withTrim(boolean trim)
Returns a newCSVFormatwith whether to trim leading and trailing blanks.- Parameters:
trim- whether to trim leading and trailing blanks.- Returns:
- A new CSVFormat that is equal to this but with the specified trim setting.
- Since:
- 1.3
-
-