Java 8简短方法:
Files.lines(Paths.get(fileName)).count();
但是大多数内存效率:
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++; } }}
您根本不需要在此任务中使用String对象。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)