Today I ran into this problem and found a simpe solution for csv files: adding an extra field containing only one space at split time:
(line + ", ").split(",");
Thus, no matter how many consecutive empty fields can exist at the end of the csv file, split () will always return n + 1 fields
Session Example (using bsh)
bsh % line = "H,\"TestItems_20100107.csv\",07/01/2010,20:00:00,\"TT1198\",\"MOBb\",\"AMD\",NEW,, bsh % System.out.println(line); H,"TestItems_20100107.csv",07/01/2010,20:00:00,"TT1198","MOBb","AMD",NEW,, bsh % String[] items = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); bsh % System.out.println(items.length); 8 bsh % items = (line + ", ").split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); bsh % System.out.println(items.length - 1 ); 10 bsh %
jee
source share