使用 scala.io.Source 的正确方法是什么?

发布于 2024-10-08 01:46:38 字数 275 浏览 0 评论 0原文

在许多示例中,描述了您可以使用 scala.io.Source 来读取整个文件,如下所示:

val str = scala.io.Source.fromFile("test.txt").mkString()

但没有提到关闭底层流。

为什么 Scala 没有提供像 Python 中的 with 子句那样方便的方法来做到这一点?看起来很有用,但并不难。

有没有其他更好的方法可以在 Scala 中安全地做到这一点,我的意思是读取整个文件?

In many examples, it is described that you can use scala.io.Source to read a whole file like this:

val str = scala.io.Source.fromFile("test.txt").mkString()

But closing the underlying stream is not mentioned.

Why does Scala not provide a convenient way to do that such as with clause in Python? It looks useful but not difficult.

Is there any other better way to do that safely in Scala, I means to read a whole file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

念﹏祤嫣 2024-10-15 01:46:38

从 Scala 2.13 开始,标准库提供了专用的资源管理实用程序:使用

在这种情况下,它可以与 < 一起使用code>scala.io.Source 因为它扩展了 AutoCloseable 以便从文件中读取数据,然后无论如何,关闭文件资源:

import scala.util.Using
import scala.io.Source

Using(Source.fromFile("file.txt")) { source => source.mkString }
// scala.util.Try[String] = Success("hello\nworld\n")

Starting Scala 2.13, the standard library provides a dedicated resource management utility: Using.

It can be used in this case with scala.io.Source as it extends AutoCloseable in order to read from a file and, no matter what, close the file resource afterwards:

import scala.util.Using
import scala.io.Source

Using(Source.fromFile("file.txt")) { source => source.mkString }
// scala.util.Try[String] = Success("hello\nworld\n")
猥琐帝 2024-10-15 01:46:38

为了完整性,

val testTxtSource = scala.io.Source.fromFile("test.txt")
val str = testTxtSource.mkString()
testTxtSource.close()

应该把事情做好。

For the sake of completeness

val testTxtSource = scala.io.Source.fromFile("test.txt")
val str = testTxtSource.mkString()
testTxtSource.close()

Should get things done.

穿透光 2024-10-15 01:46:38

Scala 的 io 库只是为了为有限的需求提供支持而进行了修改。我们努力为 Scala 提供一个经过深思熟虑的 io 库,目前托管在 assembla,还有一个 github 存储库。

如果您打算使用 I/O 除了读取短期进程上的临时文件之外,您最好使用 Java 库,或者查看编译器中当前可用的 I/O 支持(这将需要 scala) -compiler.jar 随应用程序一起分发)。

自 Scala 2.13 起,标准库 (scala.util.Using) 中提供了自动资源管理。对于较旧的 Scala 版本,请查看此问题,或查看这个库(该库在该问题的已接受答案中有所体现)。

因此,在 Scala 2.13 或更高版本上,这可以正常工作:

import scala.util.Using
import scala.io.Source

val tryStr: Try[String] = Using(Source.fromFile("test.txt"))(_.mkString())

Scala's io library was just hack done to provide support for limited needs. There was an effort to provide a well-thought io library to Scala, which is currently hosted at assembla, with a github repository as well.

If you are going to use I/O for anything more than reading the occasional file on short-lived processes, you'd better either use Java libraries, or look at the I/O support presently available in the compiler (which will require scala-compiler.jar to be distributed with the app).

Automatic resource management is provided since Scala 2.13 in the standard library (scala.util.Using). For older Scala versions look at this question, or at this library (which is featured in the accepted answer at that question).

So, on Scala 2.13 or newer, this works correctly:

import scala.util.Using
import scala.io.Source

val tryStr: Try[String] = Using(Source.fromFile("test.txt"))(_.mkString())
叫思念不要吵 2024-10-15 01:46:38

我建议使用using,这会让你的代码更整洁、更可靠

using(Source.fromFile("test.txt")){ _.mkString()}

Scala 2.13 在库中添加了 Using 。因此,附带条件是 Using.apply 返回 Try,在 Scala 2.13 上它变成:

import scala.util.Using
import scala.io.Source

Using(Source.fromFile("test.txt"))(_.mkString)

I recommend using the using, which makes your code neater and more reliable

using(Source.fromFile("test.txt")){ _.mkString()}

Scala 2.13 added Using to the library. So, with the proviso that Using.apply returns a Try, on Scala 2.13 it becomes:

import scala.util.Using
import scala.io.Source

Using(Source.fromFile("test.txt"))(_.mkString)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文