是否有任何 Scala 内置类用于捕获外部进程的输出?
由于 Scala 有很多很酷的东西,我想它可能有一些东西可以让捕获进程的输出变得容易。我知道 Java 的做法,但我考虑寻求另一种方式。
Since Scala has so many cool stuff I was thinking it may have something that makes capturing a process's output easy. I know the Java way of doing that, but I thought about asking for another way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或者有一个 repl 命令:
使用 2.8 传送任何 IO 代码都需要克服比我能击败的更多的停止能量,所以我将其全部放入编译器中。 scala.tools.nsc.io 中有很多相当有用的东西。
Or there's a repl command:
Shipping any IO code with 2.8 was going to require overcoming more stop energy than I can beat, so I put it all in the compiler. Plenty of reasonably useful stuff in scala.tools.nsc.io.
从 Scala 2.9 开始,您可以执行以下操作:
As of Scala 2.9, you can do:
甚至说 Java 这样做也是不太正确的,因为不同操作系统上的进程/流处理不同,大部分功能实际上都传递给了本机代码。
一个更有趣的问题是,Scala 是否有任何方法可以更“惯用”地与读者和作者(或渠道,如果您使用 NIO 功能包装流)合作,
答案是什么?
目前,基于 2.8 的 scala IO 库正在开发中,这几乎肯定会包含一种更好的处理流和通道的方法,但尚未可用。
您可能还会发现,在处理 exec() 进程时,演员是处理使用一个线程进行输入和一个线程进行输出的常见做法的更好方法。
It's not quite right to even say that Java does this, because of differing process/stream handling on different operating systems, much of this functionality is actually passed off to native code.
A more interesting question would then be to ask if Scala has any way to work more "idiomatically" with readers and writers (or channels if you're wrapping your streams with NIO functionality)
And the answer?
At the moment there is work underway on a scala IO library to be based on 2.8, this will almost certainly include a better way to work with streams and channels, but isn't yet available.
You might also find actors to be a better way of handling the common practice of using one thread for input and one thread for output when dealing with an exec()'d process.