Wednesday, 20 August 2014

Scala Note 14: Read and Write Files

1. Read from a file

import scala.io._
Source.fromFile(new java.io.File("input.txt")).getLines().foreach(println)

2. Write to a file
val f = new FileOutputStream("output.txt").getChannel
f write ByteBuffer.wrap("Hello".getBytes)
f close

3. Copy files

val in  = new FileInputStream("input.txt").getChannel
val out = new FileOutputStream("output.txt").getChannel
in transferTo (0, in.size, out)


No comments:

Post a Comment