package testFold
import java.io.File
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import java.util.regex.Pattern
object service {
def main(args: Array[String]) {
println(getFilesSize("C:/", "\\.(gif|png|jpg)$")/1024)
}
def getFilesSize(dir: String, fileRegex: String): Long = {
println("visit file: "+dir)
new File(dir) match {
case null => 0
case cat if cat.isFile() => if (Pattern.compile(fileRegex, Pattern.CASE_INSENSITIVE).matcher(dir).find) cat.length() else 0
case cat if cat.isDirectory() && cat.listFiles() != null =>
val tasks = cat.listFiles().map {
file => future { getFilesSize(file.getAbsolutePath(), fileRegex) }
}
val res = Await.result(Future.sequence(tasks.toSeq), 2 minutes)
res.sum
// case _ => 0
}
}
}
转载于:https://www.cnblogs.com/xiongjianjunjava/p/4341751.html