How to read only the last line of a csv file using openCSV.? Is there any way instead of reading the entire file.
Asked
Active
Viewed 3,492 times
2 Answers
2
If you know the number of lines in the cvs file then you can do the following to skip all lines before the last line:
CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', num_of_lines-1);

Morad
- 2,761
- 21
- 29
-
1Thanks. But the number of lines is not known. – user3453784 Nov 22 '14 at 18:19
0
If you don't need to use openCSV, you could use an implementation around 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.