为什么Scala Source不关闭底层InputStream? [英] Why doesn't Scala Source close the underlying InputStream?

查看:229
本文介绍了为什么Scala Source不关闭底层InputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Scala Source.fromFile 但是我似乎无法找到一个很好的方式来获取 close 底层的 InputStream



这是我的代码, code> AssertionError 因为文件不能被删除。

  def main(args:Array [String]):Unit = {

val myFile = new File(c:/tmp/doodah.txt)
var src = Source.fromFile(myFile)
src.getLines.foreach(l => print(l))

val已删除:Boolean = myFile.delete

assert(已删除,File was not deleted - maybe源文件中的流没有被关闭)

}

有一个名为 reset 的方法,但是它所做的就是从文件中重新创建源代码。



内部 Source 创建一个 BufferedSource ,它有一个 close 方法。然而,这不是从Source公开。



我希望Source一旦读取了文件的内容就释放文件句柄,但似乎没有做到这一点。

到目前为止,我所见过的最好的解决方法是从根本上将 Source 转换为 BufferedSource 并调用 close

  try {
src.getLines.foreach(l => print(l))
}
finally src match {case b:scala.io.BufferedSource => b.close}

或者我可以创建 Source InputStream ,并管理自己关闭。



然而,这似乎有些脏。如何在使用 Source 时释放文件句柄?

解决方案

Scala.io ._ 是为了支持XML库和编译器而创建的准系统。这是设计不好,有很多问题。 Scala 2.8将宣称它的改进版本,尽管几乎没有什么可写的。



有兴趣的各方正在进行第三方的努力来开发一个健全的Scala I / O库。它旨在将JDK7 I / O重新设计的经验带回家,同时提供一个Scala-ish API。

同时...每当您的应用程序发生st use关于现在图书馆的设计问题。

I'm using Scala Source.fromFile however I can't seem to find a nice way of getting it to close the underlying InputStream once the file has been read.

Here's my code that will fail with an AssertionError because the file cannot be deleted.

  def main(args : Array[String]) : Unit = {

    val myFile = new File("c:/tmp/doodah.txt")
    var src = Source.fromFile(myFile)
    src.getLines.foreach(l => print(l))

    val deleted: Boolean = myFile.delete

    assert (deleted , "File was not deleted - maybe the stream hasn't been closed in Source")

  }

Source has a method called reset however all that this does is recreate the source from the file.

Internally Source creates an underlying BufferedSource that has a close method. However this is not exposed from Source.

I'd hope that Source would release the file handle once the contents of the file had been read but it doesn't seem to do that.

The best workaround I've seen so far is to essentially cast the Source to a BufferedSource and call close.

try {
  src.getLines.foreach(l => print(l))
}
finally src match { case b: scala.io.BufferedSource => b.close }

Alternatively I could create a Source from an InputStream and manage the closing myself.

However this seems somewhat dirty. How are you supposed to release the file handle when using Source?

解决方案

Scala.io._ is a barebones hack created for the sole purpose of supporting the XML library and the compiler. It is badly designed and suffers from many problems. Scala 2.8 will purport an improved version of it, though hardly anything to write home about.

There is an on-going third party effort by interested parties to develop a sound Scala I/O library. It aims to take home the lessons learned by the JDK7 I/O redesign, while providing an Scala-ish API.

Meanwhile... use Java libraries whenever your application stumbles upon the design problems of the present library.

这篇关于为什么Scala Source不关闭底层InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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