Redirecting stdin and stdout in scala

2019-09-05 14:24发布

问题:

How can I redirect STDIN and STDOUT to files?

In C, this would be done like so:

freopen("file.in","r",stdin);

I am looking for something equivalent for Scala.

回答1:

You can do it using the Java System api. The code is nearly identical for Java and Scala:

System.setIn(new FileInputStream("file.in"))
System.setOut(new PrintStream(new FileOutputStream("file.out")))


回答2:

Do you mean something like:

import java.io.File
import scala.sys.process._
"cat" #< new File("file.in") !

source: http://www.scala-lang.org/api/2.11.7/index.html#scala.sys.process.package



标签: scala