如何将Array [String,Int]数据保存到文件中

时间:2017-05-12 18:09:47

标签: arrays scala serialization

我正在尝试将Array[String, Int]数据保存到文件中。但是,每次都报告:

  

对象不可序列化

我还尝试将两列组合成一个字符串,并希望逐行编写,但它仍然报告此类错误。代码是:

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

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

1 个答案:

答案 0 :(得分:3)

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))