Is it possible, and if yes how is it done? The usual >
and >>
that work on the Windows or Linux command line don't work in this context.
Asked
Active
Viewed 3,742 times
7

Gigatron
- 1,995
- 6
- 20
- 27
3 Answers
15
You can do it programmaticaly from console:
import java.io.FileOutputStream
import scala.Console
Console.setOut(new FileOutputStream("<output file path>"))
from now on all print
and println
would be directed into this file

Odomontois
- 15,918
- 2
- 36
- 71
-
`setOut` is not working in 2.9 REPL due to [SI-4793](https://issues.scala-lang.org/browse/SI-4793). See [my answer](http://stackoverflow.com/questions/7219316/println-vs-system-out-println-in-scala/7219519#7219519) – 4e6 Feb 29 '12 at 06:24
-
1Though not perfect, I'm accepting this is as the most useful answer. I notice sometimes there is nothing in the file until I exit the REPL. To get around that involves storing the `FileOutputStream` in a `val` and then calling `close` on it to flush the contents to the file. – Gigatron Mar 05 '12 at 01:43
-
Strangely enough this is now deprecated: ```
:82: warning: method setOut in class DeprecatedConsole is deprecated (since 2.11.0): use withOut Console.setOut(new FileOutputStream("output.txt"))``` except for the fact that it works yet ```withOut``` does not appear to be working yet... – Techmag Jun 29 '21 at 21:18
4
It's unclear from your question exactly how you want to use such a thing. An example of what you are trying to do might help.
Here's an implicit function that will add a simple operator that writes any object as a String to a file. (Note that I'm using >>
to mean unix-style >
since >
already has meaning in Scala ("less than"). You can replace this with some other operator if you like.)
implicit def anyToFileOutput(self: Any) = new {
import java.io._
def >>(filename: String) {
val f = new BufferedWriter(new FileWriter(filename))
try {
f.write(self.toString)
} finally {
if (f != null)
f.close()
}
}
}
You would use it like this:
scala> List(1,2,3) >> "out.txt"
Which produces a file, "out.txt" in the working directory containing List(1, 2, 3)

dhg
- 52,383
- 8
- 123
- 144
-
This is what I'm trying to do -- for example, if I call `calculateSomething(a,b,c)` which returns a list of `Int`s, ideally it would direct the `print` and `println` statements within `calculateSomething` to a file, and also send the returned list result to a file. – Gigatron Feb 29 '12 at 18:05
0
Looks to be working fine to me:
dcs@ayanami:~/github/scala (master)$ scala -e "println(2 * 2)" > output
dcs@ayanami:~/github/scala (master)$ cat output
4

Daniel C. Sobral
- 295,120
- 86
- 501
- 681
-
1Thanks, but that's at the OS command line, not within a running REPL session. – Gigatron Feb 29 '12 at 18:00
-
1@Gigatron Just generate strings instead of printing, and then it's easy to send to files. Then again, if you showed what you intend to accomplish instead of asking how to implement your desired solution, we all could be much more helpful. – Daniel C. Sobral Feb 29 '12 at 18:05