我有字符串的形式上傳到服務器我的CSV文件:在Java中的CSV解析:我如何分割這個令牌?
BufferedInputStream bis = new BufferedInputStream(stream);
ByteArrayBuffer baf = new ByteArrayBuffer (50);int current = 0;
int current = 0;
while ((current = bis.read()) != -1){
baf.append((byte)current);
}
String stockTxt = new String (baf.toByteArray());
String [] tokens = stockTxt.split(",");
String date_CSV = tokens [2];
String time_CSV = tokens [3];
String game_CSV = tokens [4];
String gamedesc_CSV = tokens [5];
String venue_CSV = tokens [6];
它顯示了結果:
21-June-2012 Football MU-Chelsea London (first row)
22-June-2012 Basketball NY-MY New York (second row)
當我CSV與下面的代碼解析
token [0] = 21-June-2012
token [1] = Football
token [2] = MU-Chelsea
token [3] = London 22-June-2012
token [4] = Basketball
token [5] = NY-MY
token [6] = New York
對於令牌[3]
我預計reslt是倫敦,我的令牌4預期的結果是22-June-2012
。我如何做這個分裂?
CSV代表'逗號分隔值'雖然您的記錄樣本似乎沒有任何逗號 – danielpops 2012-03-20 23:44:44
但是,它在CSV Excel文件中,所以逗號不可見。 – user1282157 2012-03-21 00:13:18
順便說一句,您應該閱讀文件通過'Reader',而不是'InputStream',因爲你對文件中的字符感興趣,而不是字節。 – 2012-03-21 00:33:19