问题
Hi : Given an arbitrary file (java), I want to count the lines.
This is easy enough, for example, using Apache's FileUtils.readLines(...) method...
However, for large files, reading a whole file in place is ludicrous (i.e. just to count lines).
One home-grown option : Create BufferedReader or use the FileUtils.lineIterator function, and count the lines.
However, I'm assuming there could be a (low memory), up to date API for doing simple large File operations with a minimal amount of boiler plate for java --- Does any such library or functionality exist anywhere in the any of the Google, Apache, etc... open-source Java utility libraries ?
回答1:
Java 8 short way:
Files.lines(Paths.get(fileName)).count();
But most memory effiecint:
try(InputStream in = new BufferedInputStream(new FileInputStream(name))){
byte[] buf = new byte[4096 * 16];
int c;
int lineCount = 0;
while ((c = in.read(buf)) > 0) {
for (int i = 0; i < c; i++) {
if (buf[i] == '\n') lineCount++;
}
}
}
You do not need String objects in this task at all.
回答2:
With Guava:
int nLines = Files.readLines(file, charset, new LineProcessor<Integer>() {
int count = 0;
Integer getResult() {
return count;
}
boolean processLine(String line) {
count++;
return true;
}
});
which won't hold the whole file in memory or anything.
回答3:
Without a library:
public static int countLines(String filename) throws IOException {
int count = 0;
BufferedReader br = new BufferedReader(new FileReader(filename));
try {
while (br.readLine() != null) count++;
} finally {
br.close();
}
return count;
}
回答4:
Here's a version that makes use of Apache Commons IO library. You can pass null
for encoding
to pick the platform default.
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
public static long countLines(String filePath, String encoding)
throws IOException {
File file = new File(filePath);
LineIterator lineIterator = FileUtils.lineIterator(file, encoding);
long lines = 0;
try {
while ( lineIterator.hasNext() ) {
lines++;
lineIterator.nextLine();
}
} finally {
LineIterator.closeQuietly( lineIterator );
}
return lines;
}
来源:https://stackoverflow.com/questions/9691420/api-for-simple-file-line-count-functions-in-java