Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 123587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:18:36+00:00 2026-05-11T04:18:36+00:00

I use huge data files, sometimes I only need to know the number of

  • 0

I use huge data files, sometimes I only need to know the number of lines in these files, usually I open them up and read them line by line until I reach the end of the file

I was wondering if there is a smarter way to do that

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:18:37+00:00Added an answer on May 11, 2026 at 4:18 am

    This is the fastest version I have found so far, about 6 times faster than readLines. On a 150MB log file this takes 0.35 seconds, versus 2.40 seconds when using readLines(). Just for fun, linux’ wc -l command takes 0.15 seconds.

    public static int countLinesOld(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();     } } 

    EDIT, 9 1/2 years later: I have practically no java experience, but anyways I have tried to benchmark this code against the LineNumberReader solution below since it bothered me that nobody did it. It seems that especially for large files my solution is faster. Although it seems to take a few runs until the optimizer does a decent job. I’ve played a bit with the code, and have produced a new version that is consistently fastest:

    public static int countLinesNew(String filename) throws IOException {     InputStream is = new BufferedInputStream(new FileInputStream(filename));     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 = 0;         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 == 0 ? 1 : count;     } finally {         is.close();     } } 

    Benchmark resuls for a 1.3GB text file, y axis in seconds. I’ve performed 100 runs with the same file, and measured each run with System.nanoTime(). You can see that countLinesOld has a few outliers, and countLinesNew has none and while it’s only a bit faster, the difference is statistically significant. LineNumberReader is clearly slower.

    Benchmark Plot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.