在Scala中,将整个文件读入内存的简单而规范的方法是什么?(理想情况下,可以控制字符编码。)

我能想到的最好的是:

scala.io.Source.fromPath("file.txt").getLines.reduceLeft(_+_)

或者我应该使用Java的一个可怕的习语,其中最好的(不使用外部库)似乎是:

import java.util.Scanner
import java.io.File
new Scanner(new File("file.txt")).useDelimiter("\\Z").next()

通过阅读邮件列表讨论,我甚至不清楚scala.io.Source是否应该是规范的I/O库。我不明白它的目的到底是什么。

... 我想要一些简单易记的东西。例如,在这些语言中,很难忘记成语……

Ruby    open("file.txt").read
Ruby    File.read("file.txt")
Python  open("file.txt").read()

当前回答

您不需要解析每一行,然后再次将它们连接起来。

Source.fromFile(path)(Codec.UTF8).mkString

我更喜欢用这个:

import scala.io.{BufferedSource, Codec, Source}
import scala.util.Try

def readFileUtf8(path: String): Try[String] = Try {
  val source: BufferedSource = Source.fromFile(path)(Codec.UTF8)
  val content = source.mkString
  source.close()
  content
}

其他回答

在scala.io.Source上使用getLines()会丢弃用于行结束符的字符(\n, \r, \r\n等)。

下面应该保持字符对字符,并且不会进行过多的字符串连接(性能问题):

def fileToString(file: File, encoding: String) = {
  val inStream = new FileInputStream(file)
  val outStream = new ByteArrayOutputStream
  try {
    var reading = true
    while ( reading ) {
      inStream.read() match {
        case -1 => reading = false
        case c => outStream.write(c)
      }
    }
    outStream.flush()
  }
  finally {
    inStream.close()
  }
  new String(outStream.toByteArray(), encoding)
}

您不需要解析每一行,然后再次将它们连接起来。

Source.fromFile(path)(Codec.UTF8).mkString

我更喜欢用这个:

import scala.io.{BufferedSource, Codec, Source}
import scala.util.Try

def readFileUtf8(path: String): Try[String] = Try {
  val source: BufferedSource = Source.fromFile(path)(Codec.UTF8)
  val content = source.mkString
  source.close()
  content
}
val lines = scala.io.Source.fromFile("file.txt").mkString

顺便说一下,“scala.”并不是真正必要的,因为它总是在作用域中,当然,您可以完全或部分地导入io的内容,而不必在前面加上“io.”。

但是,上面的操作使文件保持打开状态。为了避免问题,你应该像这样关闭它:

val source = scala.io.Source.fromFile("file.txt")
val lines = try source.mkString finally source.close()

上面代码的另一个问题是,由于它的实现,它非常慢。对于较大的文件,应该使用:

source.getLines mkString "\n"

正如一些人提到的,scala.io.Source最好避免使用,因为它会导致连接泄漏。

也许scalax和像commons-io这样的纯java库是最好的选择,直到新的孵化器项目(即scala-io)被合并。

(编辑:这在scala 2.9中不起作用,也许在2.8中也不起作用)

使用干:

scala> io.File("/etc/passwd").slurp
res0: String = 
##
# User Database
# 
... etc