Redirecting stdin and stdout in scala

后端 未结 2 516
一生所求
一生所求 2021-01-24 13:05

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 some

相关标签:
2条回答
  • 2021-01-24 13:48

    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")))
    
    0 讨论(0)
  • 2021-01-24 14:05

    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

    0 讨论(0)
提交回复
热议问题