lserlohn
lserlohn

Reputation: 6216

How to save Array[String, Int] data into file

I am trying to save a Array[String, Int] data into file. However, every time, it reports:

object not serializable

I also tried to combine the two columns into a string, and want to write it line by line, but it still report such error. The code is:

val fw = new PrintWriter(new File("/path/data_stream.txt"))

myArray.foreach(x => fw.write((x._1.toString + " " + x._2.toString + "\n").toByte

Upvotes: 0

Views: 1793

Answers (1)

Jeffrey Chung
Jeffrey Chung

Reputation: 19527

import java.nio.file._

val data = Array(("one", 1), ("two", 2), ("three", 3))
data.foreach(d => Files.write(Paths.get("/path/data_stream.txt"), (d._1 + " " + d._2 + "\n").getBytes, StandardOpenOption.CREATE, StandardOpenOption.APPEND))

Upvotes: 3

Related Questions