将CSV记录拆分为令牌的方法。
//package com.nowjava; import java.util.ArrayList; /* N o w J a v a . c o m */ public class Main { /** * Method to split the CSV record into tokens. * @param line * @return */ public static String[] splitCSVToTokens(String line) { if (!line.contains("\",") && !line.contains(",\"")) return line.split(","); ArrayList<String> tokenList = new ArrayList<String>(); String tokens[] = line.split("\",\""); String joinedTokens = null; for (int i = 0; i < tokens.length;) { /* if(i < tokens.length -1 && tokens[i].startsWith("\"") && tokens[i + 1].endsWith("\"") && !tokens[i].endsWith("\"") && !tokens[i + 1].startsWith("\"")) { joinedTokens = tokens[i] + " " + tokens[i + 1]; tokenList.add(joinedTokens.replaceFirst("^\"", "").replaceFirst("\"$", "")); i = i + 2; } else */ { tokenList.add(tokens[i].replaceFirst("^\"", "") /*来自 时代Java公众号 - nowjava.com*/ .replaceFirst