Skip to content

Instantly share code, notes, and snippets.

@bradkarels
Created June 2, 2015 13:01
Show Gist options
  • Save bradkarels/58689b864b865abe6143 to your computer and use it in GitHub Desktop.
Save bradkarels/58689b864b865abe6143 to your computer and use it in GitHub Desktop.
Write list of Strings to text file on filesystem using java.nio in Scala (2.10.5) REPL
import java.nio.file.Files
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import java.nio.file.StandardOpenOption
import collection.JavaConverters._
val utf8:Charset = StandardCharsets.UTF_8
Files.write(Paths.get("foo.txt"), "foo".getBytes(utf8))
Files.write(Paths.get("foo.txt"), "\nbar\nman".getBytes(utf8), StandardOpenOption.APPEND)
// This seems to expect java.util.List - use JavaConverter to write list items to the file.
val lines:List[String] = List("line1","line2")
Files.write(Paths.get("foolist.txt"), lines.asJava, utf8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment