Scala - A simple Scala Try, Success, Failure example Code Example

The code for A simple Scala Try, Success, Failure example

import scala.io.Source
import scala.util.{Try,Success,Failure}

object ReadFileExample extends App {
    def readFileContent(filename: String): Try[List[String]] = {
        Try(Source.fromFile(filename).getLines.toList)
    }

    val file_name = "test.txt"
    readFileContent(file_name) match {
        case Success(lines) => lines.foreach(println)
        case Failure(f) => println(f)
    }
}
Code by IncludeHelp, on August 10, 2022 22:25

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.