public ArrayList parse() {
String str;
ArrayList list = new ArrayList();
Pattern p = Pattern.compile("(\"(.*)\")|(([^,\\x0d]+))");
//Pattern p = Pattern.compile("([^\\x0d]+)");
Matcher m = p.matcher(buf);
public void readCSVFile(File CSVFile) {
int chr;
buf = new StringBuffer("");
try {
BufferedReader in = new BufferedReader(new FileReader(CSVFile));
while((chr=in.read()) != -1) {
buf.append((char)chr);
//System.out.print((char)chr);
}
in.close();
System.out.println("-------------csv file content--------------");
System.out.print(buf);
} catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
} catch (IOException ioex) {
ioex.printStackTrace();
}
}
public static void main(String[] args) throws Throwable {
File file = new File("Book1.csv");
CSVParser parser = new CSVParser();
parser.readCSVFile(file);
ArrayList list = parser.parse();
int size = list.size();
System.out.println("\n\n--------------parsed result------------------");
System.out.println("size = " + size);
for (int i = 0; i < size; i++) {
System.out.println("list[" + i + "] = " + list.get(i));
}
}
}
用notepad打开的Book1.csv内容
a,bc,"a,b",bc,c
a,ab c,c,,
a,"aaaba""aaa",ab,,
a,"a
b
c","a b ,c",d,
运行结果
-------------csv file content--------------
a,bc,"a,b",bc,c
a,ab c,c,,
a,"aaaba""aaa",ab,,
a,"a
b
c","a b ,c",d,
--------------parsed result------------------
size = 16
list[0] = a
list[1] = bc
list[2] = a,b
list[3] = bc
list[4] = c
list[5] = a
list[6] = ab c
list[7] = c
list[8] = a
list[9] = aaaba"aaa
list[10] = ab
list[11] = a
list[12] = a
b
c
list[13] = a b ,c
list[14] = d
list[15] =
/* Simple demo of CSV matching using Regular Expressions.
* Does NOT use the "CSV" class defined in the Java CookBook.
* RE Pattern from Chapter 7, Mastering Regular Expressions (p. 205, first edn.)
*/
public class CSVRE {
/** The rather involved pattern used to match CSV's consists of three
* alternations: the first matches quoted fields, the second unquoted,
* the third null fields
*/
public static final String CSV_PATTERN =
"\"([^\"\\\\]*(\\\\.[^\"\\\\]*)*)\",?|([^,]+),?|,";
// Construct a new Regular Expression parser.
Debug.println("regexp", "PATTERN = " + CSV_PATTERN); // debug
RE csv = new RE(CSV_PATTERN);
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
// For each line...
while ((line = is.readLine()) != null) {
System.out.println("line = `" + line + "'");
// For each field
for (int fieldNum = 0, offset = 0; csv.match(line, offset); fieldNum++) {
// Print the field (0=null, 1=quoted, 3=unquoted).
int n = csv.getParenCount()-1;
if (n==0) // null field
System.out.println("field[" + fieldNum + "] = `'");
else
System.out.println("field[" + fieldNum + "] = `" + csv.getParen(n) + "'");
// Skip what already matched.
offset += csv.getParen(0).length();
}
}
}
}
class Debug {
/** Static method to see if a given category of debugging is enabled.
* Enable by setting e.g., -Ddebug.fileio to debug file I/O operations.
* Use like this:<BR>
* if (Debug.isEnabled("fileio"))<BR>
* System.out.println("Starting to read file " + fileName);
*/
public static boolean isEnabled(String category) {
return System.getProperty("debug." + category) != null;
}
/** Static method to println a given message if the
* given category is enabled for debugging.
*/
public static void println(String category, String msg) {
if (isEnabled(category))
System.out.println(msg);
}
/** Same thing but for non-String objects (think of the other
* form as an optimization of this).
*/
public static void println(String category, Object stuff) {
println(category, stuff.toString());
}
}