[英]I am trying to read a csv file in java. Below is my code
final BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(file.getInputStream(entry)));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
System.out.println("line" + line);
final String[] rows = line.split(",");
this is my csv file 这是我的csv文件
" 9:42:43AM","Aug 20, 2015","RaceSummary","Page:1","Id","Race","Type","Rot.","District","PrideFor","ArtSeq","ReportSeq","Content","Type","Md","Bar Group","1","LINC ADAPTER SECTION 4","Content","N","A - ARLIN","1","1","1","Oscar James, Sr.","Content","0","<N.P.>"
i am trying to print the column which i mentioned in the csv.But i dont know why my out put is getting upto "Pride" as one line and "For" as another line like that it was repeating for the next two values ("ArtSeq","ReportSeq").Can any one suggest me where i went wrong. 我正在尝试打印我在csv中提到的列。但是我不知道为什么我的输出正变成“ Pride”作为一行,而“ For”则作为另一行,就像它在接下来的两个值(“ ArtSeq”,“ ReportSeq”)。任何人都可以建议我哪里做错了。 Thanks. 谢谢。
As you can see in your input you have second value have commas "Aug 20, 2015" this leads to more numbers of splits than that you expect. 正如您在输入中看到的那样,您拥有第二个值并且用逗号分隔“ 2015年8月20日”,这将导致拆分次数超出您的预期。 Example : You would expect this " 9:42:43AM","Aug 20, 2015" to be 2 parts but it will be three 示例:您希望此“ 9:42:43 AM”,“ 2015年8月20日”为2部分,但它将为3部分
[0]" 9:42:43AM"
[1]"Aug 20
[2] 2015"
You can change you split to be 您可以将拆分更改为
line.split("\",\"");
I believe that should solve your problem. 我相信应该可以解决您的问题。
Based on the output you provided... 根据您提供的输出...
line" 9:42:43AM","Aug 20, 2015","Race Summary","Page: 1","Id","Race","Type","Rot.","District","Pride
lineFor","Art lineSeq","Report lineSeq","Content","Type","Md","Bar Group","1","LINC ADAPTER SECTION 4","Content","N","A - ARLIN","1","1","1","Oscar James, Sr.","Content","0","<N.P.>"
Considering it is different then your input, I'd guess there might be a special character or something on the input file (for example, a tab or line spaceing). 考虑到它与您的输入不同,我猜输入文件中可能存在特殊字符或某些东西(例如,制表符或行间距)。 This is causing your while loop to read the first line (up to the line break), and then read the next line. 这导致while循环读取第一行(直到换行符),然后读取下一行。 If you put both of these onto the same line in the file it will probably work better. 如果将它们都放在文件的同一行上,则可能会更好地工作。
I should clarify as well, nothing in the code you posted would cause this behaviour, it is either somewhere else in your code or in the file itself. 我还要澄清一下,您发布的代码中没有任何内容会导致此行为,它可能是代码中的其他位置,也可能是文件本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.