Aroon Aroon - 1 year ago 65
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.

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

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:


Anybody can help me

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download