The beginning of a Scala “FileUtils” class

In production code I recommend that you use a good “Files” library like Apache Commons IO, but if you want to create your own Scala FileUtils class, here’s some source code that can help you get started.

First, here’s some code for the FileUtils class (an object, technically):

package io_examples.v1

import java.io.File
//import io_examples.common.Control.using
import scala.util.Try

object FileUtils {

    def readTextFileAsString(filename: String): Try[String] =
        Try {
            val lines = using(io.Source.fromFile(filename)) { source =>
                (for (line <- source.getLines) yield line).toList
            }
            lines.mkString("\n")
        }

    // TODO

    def copyFile(srcFile: File, destFile: File): Try[Boolean] = ???

    def readFileToByteArray(file: File): Try[Array[Byte]] = ???
    def readFileToString(file: File): Try[String] = ???
    def readFileToString(file: File, encoding: String): Try[String] = ???
    def readLines(file: File, encoding: String): Try[List[String]] = ???
    def sizeOf(file: File): Try[BigInt] = ???
    def sizeOfDirectory(directory: File): Try[BigInt] = ???

}

Here’s the using method that’s referenced in that code:

object Control {
    def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B =
        try {
            f(resource)
        } finally {
            resource.close()
        }
}

This code shows how you can use the readTextFileAsString method:

import scala.util.{Failure, Success}

object Driver extends App {

    val passwdFile = FileUtils.readTextFileAsString("/etc/passwd")
    passwdFile match {
        case Success(s) => println(s)
        case Failure(e) => println(e)
    }

}

I prefer the Try/Success/Failure approach for working with files, because it’s easy to generate exceptions with File I/O code, and I like to be able to access the reason for the failure, which you can get if you have the exception. Conversely, if you use Option/Some/None, you can’t get to the failure reason.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.