Posted By

thomasan on 11/14/11


Tagged

file write Scala


Versions (?)

Write data to a file


 / Published in: Scala
 

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

  1. def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) {
  2. val p = new java.io.PrintWriter(f)
  3. try { op(p) } finally { p.close() }
  4. }
  5.  
  6. and it's used like this:
  7.  
  8. import java.io._
  9. val data = Array("Five","strings","in","a","file!")
  10. printToFile(new File("example.txt"))(p => {
  11. data.foreach(p.println)
  12. })

Report this snippet  

You need to login to post a comment.