我正在读取 CSV 文件,并使用 OpenCSV 来读取它,并使用 CircularFifoBuffer 将数据拆分为列,并将每列的值分配给一个字符串。这对于读取 csv 文件中的特定行效果很好,但是我希望从开头开始逐行读取 csv 文件,一直向下到最后一行。
然后,每次读取一行时,都会比较字符串值,并在满足给定条件的情况下读取下一行。
我可以处理以上所有栏逐行处理 CSV 数据。
任何指示将不胜感激。
最佳答案
直接来自the FAQ :
If you want to use an Iterator style pattern, you might do something like this:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
Or, if you might just want to slurp the whole lot into a List, just call readAll()...
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
which will give you a List of String[] that you can iterate over. If all else fails, check out the Javadoc.
关于java - 使用 OpenCSV 和 FifoBuffer 逐行读取 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27877210/