计算文件中的行数-Scala [英] count number of lines in file - Scala

查看:586
本文介绍了计算文件中的行数-Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何计算类似于scala中unix命令行上的wc -l的文本文件中的行数?

How would I go about counting the number of lines in a text file similar to wc -l on the unix command line in scala?

推荐答案

io.Source.fromFile("file.txt").getLines.size

请注意,getLines返回一个Iterator[String],因此您实际上并没有将整个文件读入内存.

Note that getLines returns an Iterator[String] so you aren't actually reading the whole file into memory.

这篇关于计算文件中的行数-Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆