Write data to a file


/ Published in: Scala
Save to your folder(s)



Copy this code and paste it in your HTML
  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. })

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.