Scala--文件和正則表達式


一、讀取行

  import scala.io.Source

  val source = Source.fromFile("D:\\documents\\Scala\\MyDemo\\t.txt", "UTF-8")
  
  val lineIterator = source.getLines() //獲取文件行的迭代器
  for(l <- lineIterator){
    println(l)
  }

  val array = source.getLines().toArray  //把行放到Array中
  for(l <- array){
    println(l)
  }
  
  val context = source.mkString //把整個文件讀取成一個字符串
  println(context)
  
  source.close()

讀取文件這里不能重復讀取,只顯示一遍文本內容

111111111
22222222222222222
3333333333

 

二、讀取字符

  import scala.io.Source

  val source = Source.fromFile("D:\\documents\\Scala\\MyDemo\\t.txt","UTF-8")

  for(c <- source){  //一個字符一個字符迭代 Source擴展自 Iterator[Char]
    print(c)
  }

  val iter = source.buffered //使用bufferd方法,用head獲取字符

  while(iter.hasNext){
    if(iter.head == 'a'){
      iter.next()
      iter.next()
      print(iter.head)
    }
    else{
      iter.next()
    }
  }

 

三、讀取詞法單元和數字

 

  import scala.io.Source
  val source = Source.fromFile("D:\\sxt\\documents\\Scala\\MyDemo\\num.txt", "gbk")
  val lines = source.getLines() //以行讀取

  for(l<- lines){
    val tokens = l.split("\\s+") //將每行數據以空格隔開
    for(i<- tokens){
      print(i+",")
    }; println()

    val numbers = tokens.map(_.toDouble) //將分割的字符串轉換成double類型
    for(n<- numbers){
      print(n+"|")
    }; println()

    val numbers1 = for(n<- tokens) yield n.toDouble  //將分割的字符串轉換成double類型
    for(n <- numbers1){
      print(n+"-")
    }; println()

  }

 

四、從URL或其他源讀取

  import scala.io.Source
  val source1 = Source.fromURL("http://www.baidu.com","UTF-8")
  println(source1)
  val source2 = Source.fromString("Hello World")
  println(source2.mkString)
  val source3 = Source.stdin
  println(source3.mkString)

 

 

五、讀取二進制文件

Scala沒有讀取二進制文件的方法,需要使用Java類庫,

  import java.io.File
  val file = new File("D:\\\\sxt\\\\documents\\\\Scala\\\\MyDemo\\\\t.txt")
  val in = new FileInputStream(file)
  val bytes = new Array[Byte](file.length.toInt)

  in.read(bytes)

  for(i <- 0 until bytes.length) print(bytes(i))

  in.close()

結果:

文件內容:abcd
輸出結果:979899100

 

六、寫入文本文件

  val value = 100
  val out = new PrintWriter("t1.txt")
  for(i <- 0 to 100) out.println(i)
  out.printf("%d ", value.asInstanceOf[AnyRef]) //格式化輸出需要轉換成 AnyRef
  out.print("%d".format(value)) //通過String的format的方法完成格式化輸出
  out.close()

結果:

在項目工程目錄下,生成t1.txt文件

 

七、訪問目錄

 

 

 

八、序列化

 

 

九、進程控制

 

 

十、正則表達式

  val numPattern = "[0-9]+".r  //通過 String的 r方法

  val wsnumwsPattern = """\s+[0-9]+\s+""".r  //"""...""" 原始字符串可包含反斜線或引號,不需要轉義 "\\s+[0-9]+\\s+"

  for(matchString <- numPattern.findAllIn("99 apples, 87 bottles")) println(matchString) //findAllIn返回所有匹配項的迭代器

  val matches = numPattern.findAllIn("99 apples, 87 bottles").toArray //轉換成數組
  for(m<- matches) println(m)
  
  val m1: Option[String] = wsnumwsPattern.findFirstIn("99 apples, 87 bottles") //獲得第一個匹配項
  println(m1)

  val mp = numPattern.findPrefixOf("99 apples, 87 bottles")   //獲取開始部分匹配的
  println(mp)

  val wsmp = wsnumwsPattern.findPrefixOf("99 apples, 87 bottles")   //獲取開始部分匹配
  println(wsmp)


  val rm = numPattern.replaceAllIn("99 apples, 87 bottles","xx")   //替換所有匹配的項
  println(rm)

  val rmf = numPattern.replaceFirstIn("99 apples, 87 bottles","xx") //替換第一個匹配的項
  println(rmf)

結果:

99
87
99
87
Some( 87 )
Some(99)
None
xx apples, xx bottles
xx apples, 87 bottles

 

十一、正則表達式組

  val numitemPattern = "([0-9]+) ([a-z]+)".r  //通過()區分開,多個子表達式

  val numitemPattern(num, item) = "99 apples"   //單個匹配
  println(num,item)

  for(numitemPattern(num, item)<- numitemPattern.findAllIn("99 apples, 87 bottles")){//多個匹配項提取分組內容
    println(num,item)
  }

結果

(99,apples)
(99,apples)
(87,bottles)

 

 

參考《快學Scala》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM