scala - How can I serialize case classes to tab delimited text files? -
i have list contains case classes of same type, looking best practice in persisting text file tab delimited
case class mycase1 (x: int, y:int , name:string) val item1 = new mycase1(1,2,"item1") val item2 = new mycase1(3,4,"item2") val item3 = new mycase1(5,6,"item3") val mylist = list (item1,item2,item3)
what best way write above data structure file ? there such thing :
mylist.tofile(delimiter="\t")
because case class product
, can use productiterator
method fields in order defined in iterator. can use , map
list of mycase1
list of string
, use write file. simplified example this:
case class mycase1 (x: int, y:int , name:string) val item1 = new mycase1(1,2,"item1") val item2 = new mycase1(3,4,"item2") val item3 = new mycase1(5,6,"item3") val mylist = list (item1,item2,item3) val out = new printwriter(new filewriter(pathtomyfile)) try{ mylist.map(_.productiterator.mkstring("\t")) foreach (out.println(_)) } finally{ out.close }
the caveat fields outputted in order defined (left right) in case class constructor. if want way, solution not work.
Comments
Post a Comment