我有字符串的形式上传到服务器我的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