CsvParser
1688 ワード
import java.util.ArrayList;
public class CsvParser {
public static final String SEPARATOR = ",";
public static final char QUOTE = '"';
// parse the csv line into an ArrayList of values
public static ArrayList parse(String line) throws Exception {
ArrayList list = null;
if (line == null) {
// return null if input line is null
}
else {
list = new ArrayList();
if (line.length() == 0) {
// return an empty line if the input is an empty line
list.add(line);
}
else {
StringBuffer sb = new StringBuffer();
String[] a = line.split(SEPARATOR);
for (int i=0; i 0) { // has double-quote
if (count % 2 == 0) { // complete token
sb = new StringBuffer(sb.toString().trim());
if (sb.charAt(0) == QUOTE && sb.charAt(sb.length()-1) == QUOTE) {
sb.deleteCharAt(sb.length()-1); // remove trailing double-quote
sb.deleteCharAt(0); // remove leading double-quote
for (int h=0; h