Dave's Brain

Browse - Programming Tips - How do I write to a file with Scala?

Date: 2012nov22
Language: Scala

Q.  How do I write to a file with Scala?

A.  From my reading it seems that the Scala IO package
is fine for input but not mature for output.  So I use the Java
classes for output.  Here's a example:

def scalaWriteExample(outfilename: String): Boolean = { var writer:OutputStreamWriter = null try { val out = new FileOutputStream(new File(outfilename)) writer = new OutputStreamWriter(out, "UTF-8") } catch { case e: Exception => return false } writer.write("Something\n); writer.close() return true }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.