-1

I have a csv file. I want to write a function in Java which will tell me how many rows are there in csv. Could someone please help me in achieving this.

csv has following format:

"Time","Actual","Time","Expected","Time","Status"
"2012-09-01 00:00:00",580.543,"2012-09-01 00:00:00",570.761,"2012-09-01 01:00:00",0
"2012-09-01 01:00:00",646.703,"2012-09-01 01:00:00",672.926,"2012-09-01 02:00:00",0
"2012-09-01 02:00:00",680.705,"2012-09-01 02:00:00",687.784,"2012-09-01 03:00:00",0
"2012-09-01 03:00:00",661.968,"2012-09-01 03:00:00",702.436,"2012-09-01 04:00:00",0
Saurabh Verma
  • 627
  • 1
  • 14
  • 27
Deepak Shah
  • 175
  • 1
  • 2
  • 10

5 Answers5

8

following function counts the number of line in any file...

public int count(String filename) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    try {
    byte[] c = new byte[1024];
    int count = 0;
    int readChars = 0;
    boolean empty = true;
    while ((readChars = is.read(c)) != -1) {
        empty = false;
        for (int i = 0; i < readChars; ++i) {
            if (c[i] == '\n') {
                ++count;
            }
        }
    }
    return (count == 0 && !empty) ? 1 : count;
    } finally {
    is.close();
   }
}
Sukhdevsinh Zala
  • 1,126
  • 2
  • 14
  • 19
  • 3
    this works only if the file ends in a newLine character, otherwise the count is off by 1 – rodney757 Feb 23 '17 at 16:41
  • Keep in mind that this is not a generic solution. If a column contains data with line breaks, row count is not equal to the line count. – xmcax Apr 08 '22 at 10:25
3

Try this,

BufferedReader bufferedReader = new BufferedReader(new FileReader(FILENAME));
     String input;
     int count = 0;
     while((input = bufferedReader.readLine()) != null)
     {
         count++;
     }

     System.out.println("Count : "+count);
Thomas W
  • 13,940
  • 4
  • 58
  • 76
newuser
  • 8,338
  • 2
  • 25
  • 33
  • This will count one line too many. – Joey Aug 02 '13 at 05:44
  • You can start the `count = -1` as the first line is a header. You can also ignore blank lines. – Peter Lawrey Aug 02 '13 at 05:45
  • I don't believe in assigning non-necessary values to variables -- actually using the initial 'null' at any point would have been erroneous, so assigning a false/ invented value (when not syntactically necessary) is erroneous -- correct is to declare & leave unassigned. +1 for the BufferedReader solution, though. – Thomas W Aug 02 '13 at 05:46
  • I tried it in a seperate method so only i initialized as null. – newuser Aug 02 '13 at 05:47
2

You can count the number of lines and subtract one. Count how many times you can call BufferedReader.readLine(); You might want to ignore empty lines.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Use a regex Pattern to match newline, and count the matches?

Pattern patt = Pattern.compile("\\n");
Matcher m = patt.matcher( text);
//
int newlines = 0;
while (m.find()) {
    newlines++;
}

Count(newlines) will be one less than how many distinct lines there are. Note that your first line is headers, not data.

Thomas W
  • 13,940
  • 4
  • 58
  • 76
0

You can just read every single line separated by newline if you are sure that field are not splitted in multiple lines. Else use one of the library mentioned here.

Community
  • 1
  • 1
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69