您好,欢迎来到华佗健康网。
搜索
您的当前位置:首页scala IO按字符读取

scala IO按字符读取

来源:华佗健康网
import scala.io.{BufferedSource, Source}

object D02_按字符读取 {

  def main(args: Array[String]): Unit = {
    read1()
    // read2()
  }

  def read1(): Unit = {
    val source: BufferedSource = Source.fromFile("./data/2.txt")
    // 以字符为单位读取
    val iter: BufferedIterator[Char] = source.buffered
    while (iter.hasNext) {
      print(iter.next())
    }
    source.close()
  }

  def read2(): Unit = {
    val source: BufferedSource = Source.fromFile("./data/2.txt")
    // 若文件不大,则可以将其直接读取到一个字符串中
    val str: String = source.mkString
    println(str)
    source.close()
  }

}

2.txt内容

2.txt位置

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.com 版权所有 湘ICP备2023021991号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务