【发布时间】:2017-12-30 13:01:34
【问题描述】:
我正在按照Alvin Alexander 的教程使用Loan Pattern
这是我使用的代码 -
val year = 2016
val nationalData = {
val source = io.Source.fromFile(s"resources/Babynames/names/yob$year.txt")
// names is iterator of String, split() gives the array
//.toArray & toSeq is a slow process compare to .toSet // .toSeq gives Stream Closed error
val names = source.getLines().filter(_.nonEmpty).map(_.split(",")(0)).toSet
source.close()
names
// println(names.mkString(","))
}
println("Names " + nationalData)
val info = for (stateFile <- new java.io.File("resources/Babynames/namesbystate").list(); if stateFile.endsWith(".TXT")) yield {
val source = io.Source.fromFile("resources/Babynames/namesbystate/" + stateFile)
val names = source.getLines().filter(_.nonEmpty).map(_.split(",")).
filter(a => a(2).toInt == year).map(a => a(3)).toArray // .toSet
source.close()
(stateFile.take(2), names)
}
println(info(0)._2.size + " names from state "+ info(0)._1)
println(info(1)._2.size + " names from state "+ info(1)._1)
for ((state, sname) <- info) {
println("State: " +state + " Coverage of name in "+ year+" "+ sname.count(n => nationalData.contains(n)).toDouble / nationalData.size) // Set doesn't have length method
}
这就是我在上面的代码中应用readTextFile、readTextFileWithTry 来学习/实验上面代码中的Loan Pattern
def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B =
try {
f(resource)
} finally {
resource.close()
}
def readTextFile(filename: String): Option[List[String]] = {
try {
val lines = using(fromFile(filename)) { source =>
(for (line <- source.getLines) yield line).toList
}
Some(lines)
} catch {
case e: Exception => None
}
}
def readTextFileWithTry(filename: String): Try[List[String]] = {
Try {
val lines = using(fromFile(filename)) { source =>
(for (line <- source.getLines) yield line).toList
}
lines
}
}
val year = 2016
val data = readTextFile(s"resources/Babynames/names/yob$year.txt") match {
case Some(lines) =>
val n = lines.filter(_.nonEmpty).map(_.split(",")(0)).toSet
println(n)
case None => println("couldn't read file")
}
val data1 = readTextFileWithTry("resources/Babynames/namesbystate")
data1 match {
case Success(lines) => {
val info = for (stateFile <- data1; if stateFile.endsWith(".TXT")) yield {
val source = fromFile("resources/Babynames/namesbystate/" + stateFile)
val names = source.getLines().filter(_.nonEmpty).map(_.split(",")).
filter(a => a(2).toInt == year).map(a => a(3)).toArray // .toSet
(stateFile.take(2), names)
println(names)
}
}
但在第二种情况下,readTextFileWithTry,我收到以下错误 -
失败,消息是:java.io.FileNotFoundException: resources\Babynames\namesbystate(访问被拒绝)
我猜失败的原因来自SO我所理解的——
I am trying to open the same file on each iteration of the for loop
除此之外,我对自己的使用方式没有什么顾虑 -
- 是不是很好用?有人可以帮助我如何在多个场合使用 TRY?
- 我尝试更改
readTextFileWithTry的返回类型,例如Option[A]或Set/Map或 Scala 集合,以便稍后应用高阶函数。但无法成功。不确定这是不是一个好的做法。 - 如何在
Success的情况下使用高阶函数,因为有多个操作并且在Success的情况下代码块会变大?我不能使用Successcase 之外的任何字段。
有人可以帮我理解吗?
【问题讨论】:
标签: scala file try-catch ioexception