Class CsvPreference
- java.lang.Object
-
- org.supercsv.prefs.CsvPreference
-
public final class CsvPreference extends java.lang.ObjectBefore reading or writing CSV files, you must supply the reader/writer with some preferences.Please note: the end of line symbols are only used for writing.
Predefined configurations Constant Quote character Delimiter character End of line symbols STANDARD_PREFERENCE",\r\nEXCEL_PREFERENCE",\nEXCEL_NORTH_EUROPE_PREFERENCE";\nTAB_PREFERENCE"\t\nBy default, spaces surrounding an unquoted cell are treated as part of the data. In versions of Super CSV prior to 2.0.0 this wasn't the case, and any surrounding spaces that weren't within quotes were ignored when reading (and quotes were automatically added to Strings containing surrounding spaces when writing).
If you wish enable this functionality again, then you can create a CsvPreference with the surroundingSpacesNeedQuotes flag set to true (the default is false). This means that surrounding spaces without quotes will be trimmed when reading, and quotes will automatically be added for Strings containing surrounding spaces when writing.
You can apply the surroundingSpacesNeedQuotes property to an existing preference as follows:
private static final CsvPreference STANDARD_SURROUNDING_SPACES_NEED_QUOTES = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE).surroundingSpacesNeedQuotes(true).build();You can also create your own preferences. For example if your file was pipe-delimited, you could use the following:
Other preferences incude:private static final CsvPreference PIPE_DELIMITED = new CsvPreference.Builder('"', '|', "\n").build();- using a custom
CsvEncoderwhen writing CSV (if you want complete control over how the CSV is encoded) - using a custom
QuoteModewhen writing CSV (if you want to enable quotes when they're not normally required). You can use the existingAlwaysQuoteMode,ColumnQuoteMode, or supply your own. - using a custom
CommentMatcherto skip comments when reading CSV. You can use the existingCommentStartsWith,CommentMatches, or supply your own. - ignoring empty lines (enabled by default)
- setting the maximum number of lines a row of CSV can span (useful for debugging files with mismatched quotes)
- using a custom
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCsvPreference.BuilderBuilds immutable CsvPreference instances.
-
Field Summary
Fields Modifier and Type Field Description private CommentMatchercommentMatcherprivate intdelimiterCharprivate CsvEncoderencoderprivate java.lang.StringendOfLineSymbolsstatic CsvPreferenceEXCEL_NORTH_EUROPE_PREFERENCEReady to use configuration for north European excel CSV files (columns are separated by ";" instead of ",")static CsvPreferenceEXCEL_PREFERENCEReady to use configuration for Windows Excel exported CSV files.private booleanignoreEmptyLinesprivate intmaxLinesPerRowprivate charquoteCharprivate QuoteModequoteModestatic CsvPreferenceSTANDARD_PREFERENCEReady to use configuration that should cover 99% of all usages.private booleansurroundingSpacesNeedQuotesstatic CsvPreferenceTAB_PREFERENCEReady to use configuration for tab-delimited files.
-
Constructor Summary
Constructors Modifier Constructor Description privateCsvPreference(CsvPreference.Builder builder)Constructs a new CsvPreference from a Builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CommentMatchergetCommentMatcher()Returns the comment matcher.intgetDelimiterChar()Returns the delimiter characterCsvEncodergetEncoder()Returns the CSV encoder.java.lang.StringgetEndOfLineSymbols()Returns the end of line symbolsintgetMaxLinesPerRow()Returns the maximum number of lines a row can span.chargetQuoteChar()Returns the quote characterQuoteModegetQuoteMode()Returns the quote mode.booleanisIgnoreEmptyLines()Returns the ignoreEmptyLines flag.booleanisSurroundingSpacesNeedQuotes()Returns the surroundingSpacesNeedQuotes flag.
-
-
-
Field Detail
-
STANDARD_PREFERENCE
public static final CsvPreference STANDARD_PREFERENCE
Ready to use configuration that should cover 99% of all usages.
-
EXCEL_PREFERENCE
public static final CsvPreference EXCEL_PREFERENCE
Ready to use configuration for Windows Excel exported CSV files.
-
EXCEL_NORTH_EUROPE_PREFERENCE
public static final CsvPreference EXCEL_NORTH_EUROPE_PREFERENCE
Ready to use configuration for north European excel CSV files (columns are separated by ";" instead of ",")
-
TAB_PREFERENCE
public static final CsvPreference TAB_PREFERENCE
Ready to use configuration for tab-delimited files.
-
quoteChar
private final char quoteChar
-
delimiterChar
private final int delimiterChar
-
endOfLineSymbols
private final java.lang.String endOfLineSymbols
-
surroundingSpacesNeedQuotes
private final boolean surroundingSpacesNeedQuotes
-
ignoreEmptyLines
private final boolean ignoreEmptyLines
-
encoder
private final CsvEncoder encoder
-
quoteMode
private final QuoteMode quoteMode
-
commentMatcher
private final CommentMatcher commentMatcher
-
maxLinesPerRow
private int maxLinesPerRow
-
-
Constructor Detail
-
CsvPreference
private CsvPreference(CsvPreference.Builder builder)
Constructs a new CsvPreference from a Builder.
-
-
Method Detail
-
getDelimiterChar
public int getDelimiterChar()
Returns the delimiter character- Returns:
- the delimiter character
-
getEndOfLineSymbols
public java.lang.String getEndOfLineSymbols()
Returns the end of line symbols- Returns:
- the end of line symbols
-
getQuoteChar
public char getQuoteChar()
Returns the quote character- Returns:
- the quote character
-
isSurroundingSpacesNeedQuotes
public boolean isSurroundingSpacesNeedQuotes()
Returns the surroundingSpacesNeedQuotes flag.- Returns:
- the surroundingSpacesNeedQuotes flag
-
isIgnoreEmptyLines
public boolean isIgnoreEmptyLines()
Returns the ignoreEmptyLines flag.- Returns:
- the ignoreEmptyLines flag
-
getEncoder
public CsvEncoder getEncoder()
Returns the CSV encoder.- Returns:
- the CSV encoder
-
getQuoteMode
public QuoteMode getQuoteMode()
Returns the quote mode.- Returns:
- the quote mode
-
getCommentMatcher
public CommentMatcher getCommentMatcher()
Returns the comment matcher.- Returns:
- the comment matcher
-
getMaxLinesPerRow
public int getMaxLinesPerRow()
Returns the maximum number of lines a row can span.- Returns:
- the maximum number of lines a row can span
-
-