【发布时间】:2012-03-20 23:35:29
【问题描述】:
我的 CSV 文件中有字符串,格式为:
21-June-2012 Football MU-Chelsea London (first row)
22-June-2012 Basketball NY-MY New York (second row)
当我使用以下代码进行 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];
显示结果:
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],我的预期结果是伦敦,而对于令牌 4,我的预期结果是 22-June-2012。我该如何进行拆分?
【问题讨论】:
-
CSV 代表“逗号分隔值”,尽管您的记录样本似乎没有任何逗号
-
Daniel,它在 CSV Excel 文件中,所以逗号是不可见的..
-
顺便说一句,你应该通过
Reader而不是InputStream来读取文件,因为你对文件中的字符感兴趣,而不是字节。