Scala Snippets - Read/Write to a file

1 minute read

Reading/Writing to a file - Scala I/O

Scala doesnโ€™t provide any native class/library support when it comes to writing output to a file or reading a file. So we borrow java I/O class in scala, create a new instance of PrintWriter and pass a new File object to it:

import        // to write to the file

//create a new file
val writer = new PrintWriter(new File("demo1.txt"))

//call the method write() on the object we created 
writer.write("This is a demo")

//close it

concise way of writing a file in scala

new PrintWriter("filename") { write("file contents"); close }

while this is a one liner, it is unsafe. The approach looks nice, it is neither exception-safe nor encoding-safe. If an exception happens in write(), close will never be called, and the file wonโ€™t be closed.

PrintWriter also uses the default system encoding, which is very bad for portability. And finally, this approach generates a separate class specifically for this line. Scala already generates tons of classes even for a simple code.

Consuming a file in scala

//Read a file
//to read a file, call the fromFile() method of class Source, with the filename as argument
import  // to read a file
Source.fromFile("demo1.txt").mkString   // reads into a single line"file.txt").getLines.reduceLeft(_+_)

The above leaves the file open, however. To avoid problems, you should close it like this:

val source ="file.txt")
val lines = try source.mkString finally source.close()

Another problem with the code above is that it is horrible slow due to its implementation nature. For larger files one should use:

source.getLines mkString "\n"

Refrain from reading an entire file into a single String. The getLines method returns a value of type Iterator[String]. Itโ€™s effectively a lazy cursor into the file, allowing you to examine just the data you need without risking memory glut.

//to read individual lines instead of the whole file at once, use the getLines() method

//iterator can also be used to get one line at a time
val it = Source.fromFile("demo1.txt").getLines()

//method take(n) to return the first n values from the iterator

//print all the lines using while loop
while(it.hasNext){ print( }

//method slice(start,until) returns an iterator over lines start to until-1.