Scala 文件操作、继承与多态编程
1. Scala 文件操作
1.1 读取文本文件
在 Scala 中,我们可以使用 scala.io.Source
类来读取文本文件。以下是一个示例代码:
import java.io.{IOException, FileNotFoundException}
import scala.io.Source
object ReadFromTextFile {
def main(args: Array[String]): Unit = {
val fileName = "/Users/.../temp/InputFile.txt"
var source: scala.io.BufferedSource = null
try {
source = Source.fromFile(fileName)
for (line <- source.getLines()) {
println(line)
}
} catch {
case e: FileNotFoundException =>
println("File not found.")
case e: IOException => println("IO problem.")
case e: Exception => println("Something went wrong.")
} finally {
source.clos