(Scala 15) 文件和正则表达式

博客围绕Scala中的文件和正则表达式展开,虽未给出具体内容,但可知聚焦于Scala在文件操作及正则表达式运用方面的信息技术知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
  * 第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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值