在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()

当前回答

还有一个:https://github.com/pathikrit/better-files#streams-and-codecs

有多种方法可以在不将内容加载到内存的情况下获取文件:

val bytes  : Iterator[Byte]            = file.bytes
val chars  : Iterator[Char]            = file.chars
val lines  : Iterator[String]          = file.lines
val source : scala.io.BufferedSource   = file.content 

你也可以为任何执行读/写操作的东西提供你自己的编解码器(如果你没有提供,它会假设scala.io.Codec.default):

val content: String = file.contentAsString  // default codec
// custom codec:
import scala.io.Codec
file.contentAsString(Codec.ISO8859)
//or
import scala.io.Codec.string2codec
file.write("hello world")(codec = "US-ASCII")

其他回答

正如一些人提到的,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

为了更快地读取/上传(大)文件,可以考虑增加bufferSize (Source. size)的大小。DefaultBufSize设置为2048),例如:

val file = new java.io.File("myFilename")
io.Source.fromFile(file, bufferSize = Source.DefaultBufSize * 2)

注意Source.scala。有关进一步讨论,请参阅Scala快速文本文件读取并上载到内存。

如果您不介意第三方依赖,您应该考虑使用我的OS-Lib库。这使得读取/写入文件和使用文件系统非常方便:

// Make sure working directory exists and is empty
val wd = os.pwd/"out"/"splash"
os.remove.all(wd)
os.makeDir.all(wd)

// Read/write files
os.write(wd/"file.txt", "hello")
os.read(wd/"file.txt") ==> "hello"

// Perform filesystem operations
os.copy(wd/"file.txt", wd/"copied.txt")
os.list(wd) ==> Seq(wd/"copied.txt", wd/"file.txt")

使用单行帮助程序,用于读取字节、读取块、读取行和许多其他有用/常见操作

就像在Java中,使用CommonsIO库:

FileUtils.readFileToString(file, StandardCharsets.UTF_8)

此外,这里的许多答案忘记了Charset。最好总是显式地提供它,否则总有一天会出事。