Skip to content

Instantly share code, notes, and snippets.

@lossyrob
Last active August 14, 2019 07:54
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lossyrob/45695e44601d6a9e9077 to your computer and use it in GitHub Desktop.
Save lossyrob/45695e44601d6a9e9077 to your computer and use it in GitHub Desktop.
Read \ Write a text file in one line Scala
def write(path: String, txt: String): Unit = {
import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets
Files.write(Paths.get(path), txt.getBytes(StandardCharsets.UTF_8))
}
def read(path: String): String =
scala.io.Source.fromFile(path, "UTF-8").getLines.mkString
@soulmachine
Copy link

Looks nice, I'm also using the same way to read and write text files. Since you're writing files with UTF8 encoding, you should also read files using UTF8 encoding, scala.io.Source.fromFile(path, "UTF-8").mkString

@lossyrob
Copy link
Author

lossyrob commented Jan 4, 2016

Thanks @soulmachine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment