0

user
u1
Khulbe
Sharma
gupta

These are the entries in the CSV file i want to know if there is a Java inbuilt function that can directly give me the number of lines in CSV file? In this case, it would be 5 lines.

ankit
  • 1
  • 1
  • 2

3 Answers3

4

A built-in functionality would be to open the file and read how many lines there are. In Java8 for example, it would look like this:

final Path path = Paths.get(ClassLoader.getSystemResource("your.csv").toURI());
return Files.lines(path).skip(1L).count(); // skip(1L) to ignore the titles
Martin Seeler
  • 6,874
  • 3
  • 33
  • 45
2

CSV has nothing to do with it. All you need is a line count. This can be accomplished in two lines of code with a LineNumberReader:

lineNumberReader.skip(Long.MAX_VALUE);
int lines = lineNumberReader.getLineNumber();
user207421
  • 305,947
  • 44
  • 307
  • 483
0

Super CSV. Have a close look at Example CSV file and the output.

You can put a variable to keep count while using Super CSV to find the number of lines. Only thing you have to do is to add the jar to your classpath and then access its methods

Sufian
  • 6,405
  • 16
  • 66
  • 120
Subhrajyoti Das
  • 2,685
  • 3
  • 21
  • 36