import java.io.PrintWriter import scala.io.Source object ddd1 {def main(args: Array[String]): Unit = {//读入文件内容val content = Source.fromFile("dd.txt").mkStringprintln(content)//2.把字符串拆分为一个一个的单词,保存到数组//正则表达式//\\:表示正则表达式// w:表示一个非字(不是一个字,例如:空格,逗号,句号)//w+:多个非字val arr = content.split("\\w+")for(word <-arr){println(word)}//3.统计每个单词出现的次数val wordMap = scala.collection.mutable.Map[String,Int]()for(w <-arr){val word = w.toLowerCase()//把单词全部小写if(wordMap.contains(word)){wordMap(word)+=1}else{wordMap(word)=1}}//排序,Map是无序,要做排序,先要把数组转成数列,List,Arrayprintln( wordMap.toList)val orderWordList = wordMap.toList.sortWith((a,b)=>a._2>b._2).filter(e=>e._1,length>2).slice(0,30)//5.把结果写到一个文件中for(e<-orderWordList){println(e)}val writer= new PrintWriter("out.txt")writer.println("结果是:")writer.println("the:20次")writer.close()}}