Q:

How to write list elements to a file in scala?

I have a list l2 and I want to write the list elements to the file. I'm doing this:
val fw = new FileWriter("src/results.txt", true) ;
for( k <- 1 to l2.length) {
     println(l2(i))
     fw.write(l2(i))
     i+=1
 }
fw.close()

But, it does not write anything. Why is that ?

A:

If l2 is List(1,2,3), you are calling .write(int c) of Writer class.
That means 01 02 03 in hex binary is written to file. You can confirm it with hexdump -C src/results.txt
To call .write(String str), try fw.write(l2(i).toString).
See https://docs.oracle.com/javase/7/docs/api/java/io/Writer.html

