从文件中读取信息
在scala中,涉及到文件读写的方法有很多,可以使用java.io下的工具包,也可以使用scala.io下的功能。
source.fromFile方法
格式:scala.io.Source.formFile(文件名)
作用:读入一个文件
代码示范:
package words
object words01 {
def main(args: Array[String]): Unit = {
// 1. 读入paper.txt的内容 大大的长长的字符串
// Source.fromFile().mkString
// 2. 对字符串进行拆分 把句子变成一个一个单词
// split
// 1
val content=scala.io.Source.fromFile("./test.txt").mkString
println(content)
// 2. 对字符串进行拆分:
val rst =content.split(' ')
// ["I", "like","scala."]
rst.foreach(println)
}
}
写拆分单词统计个数
1. 分隔出一个一个的单词。
2. 建立一个Map,key是单词,value是次数。
3. 遍历所有的单词,对每个单词来说:
判断单词是否存在,如果存在把对应的key+1;
否则就设置对应的key,且value为1
代码示范:
package words
object words01 {
def main(args: Array[String]): Unit = {
// 1. 读入paper.txt的内容 大大的长长的字符串
// Source.fromFile().mkString
// 2. 对字符串进行拆分 把句子变成一个一个单词
// split
// 1
val content=scala.io.Source.fromFile("./test.txt").mkString
println(content)
// 2. 对字符串进行拆分:
val rst =content.split(' ')
// ["I", "like","scala.","I" , "am","learning"]
// 3.空的容器保存键值对数据: 键:单词,值:次数
val map=scala.collection.mutable.Map[String,Int]()
rst.foreach(word =>{
// 如果word在map中不存,就把值设为1
if(map.contains(word)){
map(word) +=1
}
else{
//map +=word ->1
map(word)=1
}
// 如果word在map中不存,就把值设为1
// println(word,map.contains(word))
})
// 4. 打印统计结果
map.foreach(println)
}
}
对Map结果排序
Map本身是键值对,它是无序的。可以先转成List或者Seq,然后再排序。
结果写入
write:不会自动换行,可以加入\n; println:写入一行
完整代码:
package words
import java.io.FileWriter
object words01 {
def main(args: Array[String]): Unit = {
// 1. 读入paper.txt的内容 大大的长长的字符串
// Source.fromFile().mkString
// 2. 对字符串进行拆分 把句子变成一个一个单词
// split
// 1
val content=scala.io.Source.fromFile("./test.txt").mkString
println(content)
// 2. 对字符串进行拆分:使用split空格,对这个长长的字符串做拆分,得到一个数组,每个元素都是一个单词
// \W+ 正则表达式,W表示一个非字符(空格,符号,!。:;) +表示一个及以上
// map 把每个单词都变小写
val rst =content.split("\W+").map(ele=>ele.toLowerCase)
// ["I", "like","scala.","I" , "am","learning"]
// 投票,画正字
// 3.空的容器保存键值对数据: 键:单词,值:次数
val map=scala.collection.mutable.Map[String,Int]()
rst.foreach(word =>{
// 如果word在map中不存,就把值+1
if(map.contains(word)){
map(word) +=1
}
else{
//map +=word ->1
map(word)=1
}
// 如果word在map中不存,就把值设为1
// println(word,map.contains(word))
})
// 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()
}
}
运行结果:
we: 11
and: 10
that: 7
to: 7
our: 7
is: 7
the: 6
i: 6
a: 6
me: 5
friendship: 5
in: 5
her: 5
lily: 4
about: 4
always: 3
day: 3
together: 3
have: 3
by: 3
it: 3
forever: 2
with: 2
better: 2
also: 2
was: 2
make: 2
just: 2
stronger: 2
not: 2
can: 2
important: 2
kind: 2
textbook: 2
us: 2
good: 2
feel: 2
project: 2
but: 2
comfort: 2
side: 2
so: 2
how: 2
giving: 2
other: 2
something: 2
made: 2
friend: 2
when: 2
gift: 2
taking: 1
walk: 1
happens: 1
time: 1
there: 1
build: 1
money: 1
someone: 1
what: 1
any: 1
instead: 1
sat: 1
will: 1
mistake: 1
didn: 1
on: 1
every: 1
class: 1
said: 1
end: 1
sad: 1
of: 1
patiently: 1
moments: 1
teacher: 1
teaches: 1
anything: 1
life: 1
cold: 1
dreams: 1
book: 1
no: 1
trust: 1
who: 1
support: 1
know: 1
blame: 1
true: 1
month: 1
warm: 1
she: 1
most: 1
my: 1
check: 1
goes: 1
like: 1
last: 1
read: 1
expecting: 1
realize: 1
group: 1
up: 1
talk: 1
from: 1
helped: 1
things: 1
should: 1
than: 1
school: 1
smile: 1
got: 1
experience: 1
buy: 1
small: 1
return: 1
forgot: 1
days: 1
showed: 1
t: 1
help: 1
many: 1
words: 1
precious: 1
mine: 1
lights: 1
close: 1
worried: 1
share: 1
worries: 1
listens: 1
world: 1
same: 1
matter: 1
handed: 1
be: 1
shared: 1
care: 1
math: 1
grade: 1
even: 1
books: 1
as: 1
ever: 1
morning: 1
each: 1
then: 1
ray: 1
cherish: 1
going: 1
lives: 1
cares: 1
grows: 1
you: 1
truly: 1
because: 1
understanding: 1
fix: 1
worked: 1
without: 1
bring: 1
happy: 1
sunshine: 1
named: 1