Scala计数行文件非常快

时间:2014-06-09 17:36:16

标签: scala count scala-collections

计算Scala中的文件行(使用BufferedInputStream)。

   object CountLinesScala {

  def main(args: Array[String]) {
    val c = countLines("C:/.../Motifs/mrr569.fasta")
    println(c)
  }

  def countLines(filename: String): Int = {
    val is = new BufferedInputStream(new FileInputStream(filename))
    try {
      val c = Array.ofDim[Byte](1024)
      var count = 0
      var readChars = 0
      var empty = true
      while ((readChars = is.read(c)) != -1) {
        empty = false
        for (i <- 0 until readChars if c(i) == '\n') {
          count=count +1
        }
      }
      if ((count == 0 && !empty)) 1 else count
    } finally {
      is.close()
    }
  }
}

它不起作用,为什么? 我点击运行,但没有反应,没有错误!

2 个答案:

答案 0 :(得分:0)

标准库处理得很好。

scala.io.Source.fromFile("C:/.../Motifs/mrr569.fasta").getLines.size

答案 1 :(得分:0)

您的代码无效,因为分配的类型为Unit而您正在将UnitInt( - 1)进行比较,而Unit(-1)永远不会相等,因此您的while循环永远不会退出。

更具体地说,此表达式具有类型(readChars = is.read(c))

  def doRead: Int = {
    readChars = is.read(c)
    readChars
  }

如果你想修改你的程序版本,你可以定义一个内部函数doRead

  while (doRead != -1) {
    empty = false
    for (i <- 0 until readChars if c(i) == '\n') {
      count=count +1
    }
  }

并在你的while循环中使用

def countLines(filename: String): Int = {
    val is = new BufferedInputStream(new FileInputStream(filename))
    try {
      val c = Array.ofDim[Byte](1024)
      var count = 0
      var readChars = 0
      var empty = true
      def doRead: Int = {
        readChars = is.read(c)
        readChars
      }
      while (doRead!= -1) {
        empty = false
        for (i <- 0 until readChars if c(i) == '\n') {
          count=count +1
        }
      }
      if ((count == 0 && !empty)) 1 else count
    } finally {
      is.close()
    }
  }

countLine的最终代码看起来像

scala.io.Source.fromFile("C:/.../Motifs/mrr569.fasta").getLines.size

但是我建议你不要写这样的Scala代码。正如brian所回答的,最常用的写法是使用scala标准库并编写

import scala.io.Source

object CountLinesScala {

  def main(args: Array[String]) {
    val c = Source.fromFile("C:/.../Motifs/mrr569.fasta").getLines().size
    println(c)
  }
}

然后您的原始程序将成为

{{1}}