WorldCount:
import java.io.File import scala.io.Source object WordCount{ def main(args: Array[String]): Unit ={ val dirfile = new File("F://english") val files = dirfile.listFiles// 获取文件列表 for(file <- files) println(file)//遍历文件列表 val listFiles = files.toList val wordsMap = scala.collection.mutable.Map[String,Int]()//单词映射,统计数目 //对每个文件遍历 listFiles.foreach(file => Source.fromFile(file).getLines().foreach(line => line.split(" "). //Scoure.fromFile(file).getLines()读取文件获取每一行 //line => line.split(" ")分割成单词 //对line.split(" ")集合遍历 foreach( word=>{ if(wordsMap.contains(word)){ wordsMap(word)+=1 }else{ wordsMap+=(word->1) } }//进行单词统计 ) )) println(wordsMap) for((key,value)<-wordsMap) println(key+": "+value) } }