简体   繁体   English

在Java中使用openCSV读取CSV文件的最后一行

[英]Reading the last line of CSV file using openCSV in Java

How to read only the last line of a csv file using openCSV.? 如何使用openCSV读取csv文件的最后一行? Is there any way instead of reading the entire file. 有什么办法可以代替读取整个文件。

如果知道cvs文件中的行数,则可以执行以下操作以跳过最后一行之前的所有行:

CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', num_of_lines-1);

If you don't need to use openCSV, you could use an implementation around RandomAccessFile . 如果您不需要使用openCSV,则可以使用RandomAccessFile周围的实现。 Here is a nice, working function (two actually, but one is more generic). 这是一个不错的工作功能 (实际上有两个,但更通用)。 Take into account the warnings in the end of that answer. 请考虑该答案末尾的警告。

Also, Apache Commons has ReversedLinesFileReader , with readLine method that "returns the lines of the file from bottom to top", though it should be tested if the entire file is not being read. 同样,Apache Commons具有ReversedLinesFileReader ,其中具有readLine方法,该方法“将文件的行从下至上返回”,但是如果没有读取整个文件,则应进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM