我創建一個實用工具類,這將使人們更容易解析一個CSV字符串並返回一個字符串數組的數組的數組的數組。CSV爲字符串
我的代碼幾乎工作,但由於某些原因,當我在第一排做我的結果弄,我期待看到1行,看到級聯的第一行上幾行。
簡單的例子:
a,b,c,d
e,f,g,h
期待:{a,b,c,d}, {e,f,g,h}
結果:{a,b,c,d,e,f,g}
public class csvParse
{
protected String originalCSV;
protected int skipToLine;
protected ArrayList<ArrayList<String>> parsedList;
public ArrayList<ArrayList<String>> getParsedList()
{
return parsedList;
}
public void setParsedList(ArrayList<ArrayList<String>> parsedList)
{
this.parsedList = parsedList;
}
public csvParse(String incomingCSV, int skipToLine)
{
super();
this.originalCSV = incomingCSV;
this.skipToLine = skipToLine;
this.parsedList = new ArrayList<ArrayList<String>>();
execute();
}
protected void execute()
{
// breaking this so there's an error. read below
//TODO: Make sure you have data out to X. May use a try/catch?
String row;
String lines[] = this.originalCSV.split("\\n?\\r");
ArrayList<String> temp = new ArrayList<String>();
try{
for (int i = this.skipToLine; i < lines.length; i++)
{
row = lines[i];
//split on commas
String[] RowData = row.split(",");
for (int x = 0; x < RowData.length; x++)
{
temp.add(RowData[x]);
}
this.parsedList.add(temp);
}
}
finally{
}
}
}
又有什麼問題? – pickypg 2011-06-16 15:31:50
incomingCSV如何生成? – Atreys 2011-06-16 15:33:49
嘗試魔'按Ctrl-Shift鍵F' – 2011-06-16 16:49:02