private int countLines() {
        InputStream is = IO.buffered(IO.inputStream(file));
        try {
            byte[] c = new byte[1024];

            int readChars = is.read(c);
            if (readChars == -1) {
                // bail out if nothing to read
                return 0;
            }

            // make it easy for the optimizer to tune this loop
            int count = 1;
            while (readChars == 1024) {
                for (int i = 0; i < 1024; ) {
                    if (c[i++] == '\n') {
                        ++count;
                    }
                }
                readChars = is.read(c);
            }

            // count remaining characters
            while (readChars != -1) {
                for (int i = 0; i < readChars; ++i) {
                    if (c[i] == '\n') {
                        ++count;
                    }
                }
                readChars = is.read(c);
            }

            return count;
        } catch (IOException e) {
            throw E.ioException(e);
        } finally {
            IO.close(is);
        }
    }
private int countLines ( ) { InputStream is = IO . buffered ( IO . inputStream ( file ) ) ; try { byte [ ] c = new byte [ 1024 ] ; int readChars = is . read ( c ) ; if ( readChars == - 1 ) { return 0 ; } int count = 1 ; while ( readChars == 1024 ) { for ( int i = 0 ; i < 1024 ; ) { if ( c [ i ++ ] == '\n' ) { ++ count ; } } readChars = is . read ( c ) ; } while ( readChars != - 1 ) { for ( int i = 0 ; i < readChars ; ++ i ) { if ( c [ i ] == '\n' ) { ++ count ; } } readChars = is . read ( c ) ; } return count ; } catch ( IOException e ) { throw E . ioException ( e ) ; } finally { IO . close ( is ) ; } }
see https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java