Number of Lines in a File in Java

Number of lines in a file in Java

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) {
System.out.println(readChars);
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

How can I get the count of line in a file in an efficient way?

BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();

Update: To answer the performance-question raised here, I made a measurement. First thing: 20.000 lines are too few, to get the program running for a noticeable time. I created a text-file with 5 million lines. This solution (started with java without parameters like -server or -XX-options) needed around 11 seconds on my box. The same with wc -l (UNIX command-line-tool to count lines), 11 seconds. The solution reading every single character and looking for '\n' needed 104 seconds, 9-10 times as much.

How to get the number of lines from a text file in java?

If you are using 1.7+, You can directly store to array like this.

import java.io.File;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

import java.util.List;

// more code

Path filePath = new File("fileName").toPath();
Charset charset = Charset.defaultCharset();
List<String> stringList = Files.readAllLines(filePath, charset);
String[] stringArray = stringList.toArray(new String[]{});

Java NIO Files count() Method for Counting the Number of Lines

You can use try-with-resources

long numberofLines = 0;
try (Stream<String> stream = Files.lines(filePath)) {
numberOfLines = stream.count();
} catch (IOException ex) {
//catch exception
}

Then you can continue with your logic but the stream should already be closed.

How to get the number of lines from a text file in java ignoring blank lines?

Files.lines returns a Stream<String>. You can therefore use the same filtering operations as you can on any other stream:

long count = Files.lines(Paths.get("lorem.txt")).filter(line -> line.length() > 0).count();

counting the number of lines in a text file (java)

If you just want to add the data to an array, then I append the new values to an array. If the amount of data you are reading isn't large and you don't need to do it often that should be fine. I use something like this, as given in this answer: Reading a plain text file in Java

BufferedReader fileReader = new BufferedReader(new FileReader("path/to/file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();

while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}

If you are reading in numbers, the strings can be converted to numbers, say for integers intValue = Integer.parseInt(text)

Count the lines of a file

By searching on the Internet you could have found this by yourself.
But nevertheless, here is a code that I use:

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

I hope it works for you.

Update:

This should be easier for you.

BufferedReader reader = new BufferedReader(new FileReader(file));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
System.out.println(lines);

Counting the Number of Lines in File

Unless your file ends with a line that contains the String "null", your code is wrong. You should be comparing to null, not to "null".

And regarding to your original question, you can avoid the extra count by both reading a line and testing for null in the condition of the loop :

while((line = bufferedReader.readLine()) != null) {
a = a + 1;
System.out.println(a + " " + line);
}


Related Topics



Leave a reply



Submit