[英]Why does Scanner read every other line of CSV file? Java
I am reading in a CSV file and putting each delimited element into a two-dimensional array. 我正在读取CSV文件,并将每个定界元素放入二维数组中。 The code looks like this:
代码如下:
public DataProcess(String filename, String[][] contents, int n) {//n is 6 for contents, 5 for fiveMinContents
Scanner fileReader = null;
try {
fileReader = new Scanner(new File(filename));
} catch (FileNotFoundException ex) {
System.out.println(ex + " FILE NOT FOUND ");
}
fileReader.useDelimiter(",");
int rowIndex = 0;
while (fileReader.hasNext()) {
for (int j = 0; j < n; j++) {
contents[rowIndex][j] = fileReader.next();
System.out.println("At (" + rowIndex +", "+j+"): " +
contents[rowIndex][j]);
}
rowIndex++;
fileReader.nextLine();
}
}
I am not sure why it reads every other line of this particular CSV file because this is file 2/2 that is being read in this manner. 我不确定为什么它会读取此特定CSV文件的其他所有行,因为这是以这种方式读取的文件2/2。 The first one reads fine, but now this one skips every other line.
第一个没问题,但是现在这个跳过了其他每一行。 Why would it work for one but not the other?
为什么它对一个有效,但对另一个无效? I am running this on Eclipse's latest update.
我正在Eclipse的最新更新上运行它。
I also checked out this answer and it did not help. 我也签出了这个答案,它没有帮助。
Because the last line of your loop reads a line and discards it. 因为循环的最后一行将读取并丢弃该行。 You need something like,
您需要类似的东西,
while (fileReader.hasNextLine()) {
String line = fileReader.nextLine();
contents[rowIndex] = fileReader.split(",\\s*");
System.out.println("At (" + rowIndex + "): "
+ Arrays.toString(contents[rowIndex]));
rowIndex++;
}
You could also print the multi-dimensional array with one call like 您也可以通过一个调用来打印多维数组,例如
System.out.println(Arrays.deepToString(contents));
While the approach may work for you, it's not optimal. 尽管该方法可能对您有用,但这并不是最佳选择。 There are premade CSV readers for Java.
有用于Java的预制CSV阅读器。 One example is commons-csv :
一个例子是commons-csv :
Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
String date = record.get(1);
String time = record.get(2);
// and so on, so forth
}
There are a small number of dependencies that have to be on your classpath. 在类路径上必须有少量依赖项 。 Hope that helps.
希望能有所帮助。
I found the issue to this problem. 我找到了这个问题的问题。
First, I recommend using the external library that was suggested. 首先,我建议使用建议的外部库。
The issue was that since this second file was reading the entire row, whereas the first CSV file was reading what I wanted it to, but there was a column at the end of the file that I was ignoring. 问题在于,由于第二个文件正在读取整行,而第一个CSV文件正在读取我想要的内容,但是文件末尾有一列我忽略了。 There must be a way that a CSV file is structured where the end of a row has a different delimiter or something along those lines--not sure.
一定有一种结构化CSV文件的方式,其中行的末尾具有不同的分隔符或沿这些行的内容-不确定。 To fix this issue, I just added an extra column to the second file and I am not reading it in;
为了解决这个问题,我刚刚在第二个文件中添加了一个额外的列,但我没有读进去。 it is just there.
它就在那里。
In short, use an external CSV-reader library. 简而言之,请使用外部CSV阅读器库。 If you don't want to do that, then just add a column directly after the last column in the file and do not read it.
如果您不想这样做,则只需在文件中的最后一列之后直接添加一列,而不读取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.