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位置