简体   繁体   English

我正在尝试在Java中读取csv文件。 下面是我的代码

[英]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.

相关问题 我正在尝试使用和学习Java中的关键字“ this”。 在我的代码中我遇到了一个错误 - I am trying to use and learn the keyword “this” in java. In my code i got an error 我正在尝试使用Java更新oracle数据库中的密码。 难道我做错了什么? - I'm trying to update my password in oracle database using Java. Am I doing something wrong? 尝试使用Java查找GCD。 我在逻辑上坚持做某些事情 - Trying to find the GCD using java. I am stuck on some things with my do while logic 数独-Java。 我正在尝试查看我的行是否完整。 它不能重复数字 - Sudoku - java. I am trying to see if my row is complete or not. it cannot have a number repeated 我正在尝试在Java的多维数组中对角地向上搜索。 为什么我出现索引超出范围错误? - I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error? 在Java中读取CSV文件时出现问题。 只读取第一行 - Problems reading a CSV file in Java. Only the first line is read 我是android Java的初学者。 谁能解释代码 - I am beginner in android Java. Can anyone explain the code 如何在 mysql 中创建存储过程并在 JAVA 中调用它。 我需要一些帮助来创建一个可调用的过程。 下面是我的代码 - How to create stored procedure in mysql and call it in JAVA. i need some help on creating a callable procedure. below id my code 我想使用Java获取我的Android设备的Build Number,Android版本。 我使用以下代码: - I want to get Build Number, Android Version of my Android device using Java. I am using the following code: 我有不同版本的javac和java。 我正在尝试在终端上运行.java文件。 但是它可以在日食中工作 - I have different versions of javac and java. I'm trying to run my .java file on terminal. But it works in eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM