高阶函数是将函数用作参数或返回值的函数。
创建一个hello.txt文件
import java.io.BufferedReader
import java.io.FileReader
fun main(args: Array<String>) {
ioWith()
ioReadText()
ioUse()
}
fun ioUse() {
BufferedReader(FileReader("hello.json")).use {
var line: String?
while (true) {
line = it.readLine() ?: break
println(line)
}
}
}
fun ioReadText() {
val text = BufferedReader(FileReader("hello.json")).readText()
println(text)
}
fun ioWith() {
val bufferedReader = BufferedReader(FileReader("hello.txt"))
with(bufferedReader) {
var line: String?
while (true) {
line = readLine() ?: break
println(line)
}
close()
}
}
网友评论