1. 文件读写
(一)任务分析
-
1. 把文字内容从.txt文件中读到内存中。
-
2. 写程序分析数据。
-
3. 把结果写入.txt文件。
(二)从文件中读出内容
-
**在scala中,涉及到文件读写的方法有很多,可以使用java.io下的工具包,也可以使用scala.io下的功能。
-
下面介绍source.fromFile这个方法。
-
格式:scala.io.Source.formFile(文件名)
-
作用:读入一个文件
2. 字符串拆分
【代码演示:】
package words
object words01 {
def main(args: Array[String]): Unit = {
// 1.读入test.txt的内容 ,大大的长长的字符串
// Source,formFile().mkString
// 2.对字符串进行拆分
// split
val content = scala.io.Source.fromFile("./test.txt").mkString
println(content)
// 对字符串进行拆分;
val rst = content.split(' ')
// ["I","like","scala."]
rst.foreach(println)
}
}
//返回值:
// I like scala.
// im
// form
// China,
// China
// is
// very
// funny
// I
// like
// scala.
3.写拆分单词统计个数
【思路:】
1. 分隔出一个一个的单词。
2. 建立一个Map,key是单词,value是次数。
3. 遍历所有的单词,对每个单词来说:
(判断单词是否存在,如果存在把对应的key+1;否则就设置对应的key,且value为1)
package words
object words02 {
def main(args: Array[String]): Unit = {
// 1.读入test.txt的内容 ,大大的长长的字符串
// Source,formFile().mkString
// 2.对字符串进行拆分
// split
// 1.
val content = scala.io.Source.fromFile("./test.txt").mkString
println(content)
// 2.对字符串进行拆分;使用split 空格,对这个长长的字符串做拆分,得到一个数组,每一个元素都是一个单词
val rst = content.split(' ')
// ["I","like","scala."]
// 3.空的容器保存键值对数组:键:单词, 值:次数
val map = scala.collection.mutable.Map[String,Int]()
rst.foreach(word=> {
// 如果word在map中存在,就把值+1
if(map.contains(word)){
map(word) += 1
}
else { // 如果word在map中不存在,就把值设为1
// map += word -> 1
map(word) = 1
}
})
// println(word, map.contains(word))
// 4.打印统计结果
map.foreach(println)
}
}
代码说明:字符串的split方法。
4.对Map结果排序
Map本身是键值对,它是无序的。可以先转成List或者Seq,然后再排序
package words
import java.io.FileWriter
object words02 {
def main(args: Array[String]): Unit = {
// 1.读入test.txt的内容 ,大大的长长的字符串
// Source,formFile().mkString
// 2.对字符串进行拆分
// split
// 1.
val content = scala.io.Source.fromFile("./test.txt").mkString
println(content)
// 2.对字符串进行拆分;使用split 空格,对这个长长的字符串做拆分,得到一个数组,每一个元素都是一个单词
// \W+ 正则表达式,W表示一个非字符(空格,符号,! , : ; ...) + 表示一个及以上
// 把每一个单词都变小写
val rst = content.split("\W+").map(ele=>ele.toLowerCase)
// ["I","like","scala."]
// 3.空的容器保存键值对数组:键:单词, 值:次数
val map = scala.collection.mutable.Map[String,Int]()
rst.foreach(word=> {
// 如果word在map中存在,就把值+1
if(map.contains(word)){
map(word) += 1
}
else { // 如果word在map中不存在,就把值设为1
// map += word -> 1
map(word) = 1
}
})
// 4.打印统计结果(排序)
// map.foreach(println)
// 按照单词出现的频率。从高到低排序
// map是无序的,不能做排序
// list是可以排序的
val wordLists = map.toList.sortBy(_._2).reverse
//5.把结果写入到文件中
val writer = new FileWriter("result.txt")
wordLists.foreach(ele =>{
writer.write(s"${ele._1}: ${ele._2} \n")
})
writer.close()
}
}
【write:不会自动换行,可以加入\n; println:写入一行】