/**
* 第15章 文件和正则表达式
*/
import java.io.{File, FileInputStream, PrintWriter}
import com.shuai.module_15_file_and_regex.Person15
import com.sun.xml.internal.ws.developer.Serialization
import scala.io.Source
//########################### 15.1 读取行 #################################
val source = Source.fromFile("f:/app.log", "utf-8")
val lineIterator = source.getLines()
for (line <- lineIterator) println(line)
source.close()
val source11 = Source.fromFile("f:/app.log", "utf-8")
val arr = source11.getLines().toArray
for (line <- arr) println(line)
source11.close()
//########################### 15.2 读取字符 #################################
val source2 = Source.fromFile("f:/app.log", "utf-8")
for (line <- source2) print(line + ",")
source2.close()
val source21 = Source.fromFile("f:/app.log", "utf-8")
val iter = source21.buffered
while (iter.hasNext) {
print(iter.next() + ",")
}
source21.close()
//########################### 15.3 读取词法单元和数字 #################################
/**
* 如果想读取以空格隔开的词法单元,可以使用如下:
*/
val source3 = Source.fromFile("f:/app.log", "utf-8")
val tokens = source3.mkString.split("\\s+")
//tokens: Array[String] = Array(#2019-05-18T03:24:45.337Z, tom, lily, salery, angle, baby, father, mother, grandpa)
//########################### 15.4 从URL或其他源读取 #################################
val source4 = Source.fromURL("http://spark.apache.org").mkString.split(" ")
val source41 = Source.fromString("www.baidu.com").mkString.split("\\.")
val source42 = Source.stdin //(从标准输入读取)控制台输入
//读取二进制文件,需要利用java方法
val file = new File("f:/app.log")
val in = new FileInputStream(file)
val bytes = new Array[Byte](file.length.toInt) //后面()是定义Array的长度
in.read(bytes)
in.close()
bytes
/**
* scala没有内建的对写入文件的致辞.要写入文本文件,可使用java.io.PrintWriter
*/
val out = new PrintWriter("f:/app2.log")
for (i <- 1 to 100) out.println(i)
out.close()
//########################### 15.5 序列化 #################################
/**
* 实现Serializable
* 如果想接受缺省的ID,也可以省略掉注解@SerialVersionUID
*/
@SerialVersionUID(23L) class Person(val name: String) extends Serializable {
override def toString = name
}
/**
* 进行序列化和反序列化
*/
import java.io._
//序列化
val fred = new Person15("fred")
val out2 = new ObjectOutputStream(new FileOutputStream("f:/Person.serialize"))
out2.writeObject(fred)
out.close()
//反序列化
val in2 = new ObjectInputStream(new FileInputStream("f:/Person.serialize"))
val saveFred = in2.readObject().asInstanceOf[Person15]
//########################### 15.6 正则表达式 #################################
val numPattern = "[0-9]+".r
for (matchiStirng <- numPattern.findAllIn("99sdf,23dsjo2fe234"))
print(matchiStirng + " ")
numPattern.findAllIn("99sdf,23dsjo2fe234").toArray
numPattern.findFirstIn("99sdf,23dsjo2fe234")
numPattern.replaceFirstIn("99sdf,23dsjo2fe234", "XX")
numPattern.replaceAllIn("99sdf,23dsjo2fe234", "XX")
//表达式组合
val numitemPattern = "([0-9]+) ([a-z]+)".r
//括号 提取器 原理是unapply,调用的是unapplySeq
//将num设为"99",item设为"bottles"
val numitemPattern(num, item) = "99 bottles"
for (numitemPattern(num, item) <- numitemPattern.findAllIn("99 bottles, 88 tommer"))
print(num, item) //(99,bottles)(88,tommer)