Aroon Aroon - 27 days ago 6
Scala Question

How to Append the text file using stored value variable in Scala

I want to append line at end of text file without using the loop or Array. I tried following code it gives me an error.

import scala.io.Source
import java.io._
var ValueOne:Int=15
var ValueTwo:Int=16
var ValueThree:Int=20
val fw = new FileWriter("Text File", true) ; fw.write(ValueOne.toString,ValueTwo.toString,ValueThree.toString) ;
fw.flush
fw.close()


here is the error

error:overloaded method value write with alternatives:
(x$1: String,x$2: Int,x$3: Int)Unit <and>
(x$1: Array[Char],x$2: Int,x$3: Int)Unit
cannot be applied to (String, String, String)


I wanted to write the output in this format in text file:

11,92,20
19,21,34
15,16,20


Anybody can help me

Answer

Method write takes one string as an argument, so you need to combine the values into a string:

fw.write( Seq(ValueOne, ValueTwo, ValueThree).mkString(",") )

You can do it using the mkString method.