Created
June 2, 2015 13:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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