Return to Snippet

Revision: 53129
at November 14, 2011 20:34 by thomasan


Initial Code
def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) {
  val p = new java.io.PrintWriter(f)
  try { op(p) } finally { p.close() }
}

and it's used like this:

import java.io._
val data = Array("Five","strings","in","a","file!")
printToFile(new File("example.txt"))(p => {
  data.foreach(p.println)
})

Initial URL
http://stackoverflow.com/questions/4604237/how-to-write-to-a-file-in-scala

Initial Description

                                

Initial Title
Write data to a file

Initial Tags
file

Initial Language
Scala