Skip to content

Instantly share code, notes, and snippets.

@shiguruikai
Created January 15, 2023 14:26
Show Gist options
  • Save shiguruikai/a57bf6c4019e8c1fb481b164421e4956 to your computer and use it in GitHub Desktop.
Save shiguruikai/a57bf6c4019e8c1fb481b164421e4956 to your computer and use it in GitHub Desktop.
Quickly count lines in large files. 大きなファイルの行数を素早く数える。
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class CountLines {
private static final int MAX_BUFFER_SIZE = 1024 * 1024;
public static void main(String[] args) throws Exception {
if (args.length < 1) {
System.err.println("The file path is not specified.");
System.exit(1);
}
final var filePath = Paths.get(args[0]).toAbsolutePath();
if (!Files.isRegularFile(filePath)) {
System.err.println("The specified file path does not exist.");
System.exit(1);
}
try (var channel = Files.newByteChannel(filePath, StandardOpenOption.READ)) {
final var bufferSize = (int) clamp(Files.size(filePath), 1, MAX_BUFFER_SIZE);
final var buf = ByteBuffer.allocateDirect(bufferSize);
var result = 0L;
while (channel.read(buf) != -1) {
buf.flip();
while (buf.hasRemaining()) {
if (buf.get() == '\n') {
result++;
}
}
buf.clear();
}
System.out.println(result);
}
}
private static long clamp(long value, long min, long max) {
return Math.max(min, Math.min(max, value));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment