1 import java
.util
.ArrayList
;
4 public class CSVUtils
{
6 private static final char DEFAULT_SEPARATOR
= ',';
7 private static final char DEFAULT_QUOTE
= '"';
9 public static List
<String
> parseLine(String csvLine
) {
10 return parseLine(csvLine
, DEFAULT_SEPARATOR
, DEFAULT_QUOTE
);
13 public static List
<String
> parseLine(String csvLine
, char separators
) {
14 return parseLine(csvLine
, separators
, DEFAULT_QUOTE
);
17 public static List
<String
> parseLine(String csvLine
, char separators
, char customQuote
) {
19 List
<String
> result
= new ArrayList
<>();
22 if (csvLine
== null && csvLine
.isEmpty()) {
26 if (customQuote
== ' ') {
27 customQuote
= DEFAULT_QUOTE
;
30 if (separators
== ' ') {
31 separators
= DEFAULT_SEPARATOR
;
34 StringBuffer curVal
= new StringBuffer();
35 boolean inQuotes
= false;
36 boolean startCollectChar
= false;
37 boolean doubleQuotesInColumn
= false;
39 char[] chars
= csvLine
.toCharArray();
41 for (char ch
: chars
) {
44 startCollectChar
= true;
45 if (ch
== customQuote
) {
47 doubleQuotesInColumn
= false;
50 //Fixed : allow "" in custom quote enclosed
52 if (!doubleQuotesInColumn
) {
54 doubleQuotesInColumn
= true;
62 if (ch
== customQuote
) {
66 //Fixed : allow "" in empty quote enclosed
67 if (chars
[0] != '"' && customQuote
== '\"') {
71 //double quotes in column will hit this!
72 if (startCollectChar
) {
76 } else if (ch
== separators
) {
78 result
.add(curVal
.toString());
80 curVal
= new StringBuffer();
81 startCollectChar
= false;
83 } else if (ch
== '\r') {
84 //ignore LF characters
86 } else if (ch
== '\n') {
96 result
.add(curVal
.toString());