Skip to content

Instantly share code, notes, and snippets.

@ikenna
Created January 14, 2015 13:19
Show Gist options
  • Save ikenna/248aef91363893149e29 to your computer and use it in GitHub Desktop.
Save ikenna/248aef91363893149e29 to your computer and use it in GitHub Desktop.
Write to a file in Scala
def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) {
val p = new java.io.PrintWriter(f)
try { op(p) } finally { p.close() }
}
//Usage
printToFile(new File("fileName.txt"))(p => p.write("someString"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment