Scala: write string to file in one statement
A concise one line:
import java.io.PrintWriter
new PrintWriter("filename") { write("file contents"); close }
It is strange that no one had suggested NIO.2 operations (available since Java 7):
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
Files.write(Paths.get("file.txt"), "file contents".getBytes(StandardCharsets.UTF_8))
I think this is by far the simplest and easiest and most idiomatic way, and it does not need any dependencies sans Java itself.
Here is a concise one-liner using reflect.io.file, this works with Scala 2.12:
reflect.io.File("filename").writeAll("hello world")
Alternatively, if you want to use the Java libraries you can do this hack:
Some(new PrintWriter("filename")).foreach{p => p.write("hello world"); p.close}