Kotlin实战指南十九:use 函数魔法

转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/117366756
本文出自【赵彦军的博客】

往期精彩文章

Kotlin实战指南十八:open、internal 关键字使用
Kotlin实战指南十七:JvmField、JvmStatic使用
Kotlin实战指南十六:Synchronized、Volatile

本文章转载于:Kotlin use函数的魔法

use函数

  • 实现了Closeable接口的对象可调用use函数
  • use函数会自动关闭调用者(无论中间是否出现异常)

在这里插入图片描述
在这里插入图片描述

  • 可以看出,use 函数内部实现也是通过 try-catch-finally 块捕捉的方式,所以不用担心会有异常抛出导致程序退出
  • close 操作在finally里面执行,所以无论是正常结束还是出现异常,都能正确关闭调用者

下面我们就对比一下 Java 和 Kotlin 实现的不同

Java 版本

//Java 实现
FileInputStream fis = null;
DataInputStream dis = null;
try {
    fis = new FileInputStream("/home/test.txt");
    dis = new DataInputStream(new BufferedInputStream(fis));
    String lines = "";
    while((lines = dis.readLine()) != null){
        System.out.println(lines);
    }
} catch (IOException e){
    e.printStackTrace();
} finally {
    try {
        if(dis != null)
            dis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if(fis != null)
            fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Kotlin 版本

File("/home/test.txt").readLines()
        .forEach { println(it) }

Kotlin 就是可以两行实现。

仔细翻阅 readLines 这个扩展函数的实现你会发现,它也是间接调用 use,这样就省去了捕捉异常和关闭的烦恼
同样的,经过包装以后你只需要关注读出来的数据本身而不需要 care 各种异常情况

  • File的一些其它有用的扩展函数
/**
* 将文件里的所有数据以字节数组的形式读出
* Tip:显然这不适用于大文件,文件过大,会导致创建一个超大数组
*/
public fun File.readBytes(): ByteArray

/**
* 与上一个函数类似,不过这个是写(如果文件存在,则覆盖)
*/
public fun File.writeBytes(array: ByteArray)Unit

/**
* 将array数组中的数据添加到文件里(如果文件存在则在文件尾部添加)
*/
public fun File.appendBytes(array: ByteArray): Unit


/**
* 将文件以指定buffer大小,分块读出(适用于大文件,也是最常用的方法)
*/
public fun File.forEachBlock(action: (buffer: ByteArray, bytesRead: Int) -> Unit): Unit

/**
* Gets the entire content of this file as a String using UTF-8 or specified [charset].
*
* This method is not recommended on huge files. It has an internal limitation of 2 GB file size.
*
* @param charset character set to use.
* @return the entire content of this file as a String.
*/
public fun File.readText(charset: Charset = Charsets.UTF_8): String

/**
* Sets the content of this file as [text] encoded using UTF-8 or specified [charset].
* If this file exists, it becomes overwritten.
*
* @param text text to write into file.
* @param charset character set to use.
*/
public fun File.writeText(text: String, charset: Charset = Charsets.UTF_8): Unit

/**
* Appends [text] to the content of this file using UTF-8 or the specified [charset].
*
* @param text text to append to file.
* @param charset character set to use.
*/
public fun File.appendText(text: String, charset: Charset = Charsets.UTF_8): Unit

/**
* Reads this file line by line using the specified [charset] and calls [action] for each line.
* Default charset is UTF-8.
*
* You may use this function on huge files.
*
* @param charset character set to use.
* @param action function to process file lines.
*/
public fun File.forEachLine(charset: Charset = Charsets.UTF_8, action: (line: String) -> Unit): Unit


/**
* Reads the file content as a list of lines.
*
* Do not use this function for huge files.
*
* @param charset character set to use. By default uses UTF-8 charset.
* @return list of file lines.
*/
public fun File.readLines(charset: Charset = Charsets.UTF_8): List<String>


/**
* Calls the [block] callback giving it a sequence of all the lines in this file and closes the reader once
* the processing is complete.

* @param charset character set to use. By default uses UTF-8 charset.
* @return the value returned by [block].
*/
@RequireKotlin("1.2", versionKind = RequireKotlinVersionKind.COMPILER_VERSION, message = "Requires newer compiler version to be inlined correctly.")
public inline fun <T> File.useLines(charset: Charset = Charsets.UTF_8, block: (Sequence<String>) -> T): T

上面的函数都是基于use实现的,可以放心使用,而不用担心异常的发生,并且会自动关闭IO流

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值