scala 获取本地图片

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
    }
      
  }
}

posted @ 2015-03-16 14:23  xiongjianjun  阅读(458)  评论(0编辑  收藏  举报