日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/9/30 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Kotlin实战指南十九:use 函数魔法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/zhaoyanjun6/article/details/117366756
本文出自【趙彥軍的博客】

文章目錄

  • 往期精彩文章
  • use函數(shù)

往期精彩文章

Kotlin實(shí)戰(zhàn)指南十八:open、internal 關(guān)鍵字使用
Kotlin實(shí)戰(zhàn)指南十七:JvmField、JvmStatic使用
Kotlin實(shí)戰(zhàn)指南十六:Synchronized、Volatile

本文章轉(zhuǎn)載于:Kotlin use函數(shù)的魔法

use函數(shù)

  • 實(shí)現(xiàn)了Closeable接口的對(duì)象可調(diào)用use函數(shù)
  • use函數(shù)會(huì)自動(dòng)關(guān)閉調(diào)用者(無(wú)論中間是否出現(xiàn)異常)

在這里插入圖片描述

  • 可以看出,use 函數(shù)內(nèi)部實(shí)現(xiàn)也是通過(guò) try-catch-finally 塊捕捉的方式,所以不用擔(dān)心會(huì)有異常拋出導(dǎo)致程序退出
  • close 操作在finally里面執(zhí)行,所以無(wú)論是正常結(jié)束還是出現(xiàn)異常,都能正確關(guān)閉調(diào)用者

下面我們就對(duì)比一下 Java 和 Kotlin 實(shí)現(xiàn)的不同

Java 版本

//Java 實(shí)現(xiàn) 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) }

對(duì) Kotlin 就是可以?xún)尚袑?shí)現(xiàn)。

仔細(xì)翻閱 readLines 這個(gè)擴(kuò)展函數(shù)的實(shí)現(xiàn)你會(huì)發(fā)現(xiàn),它也是間接調(diào)用 use,這樣就省去了捕捉異常和關(guān)閉的煩惱
同樣的,經(jīng)過(guò)包裝以后你只需要關(guān)注讀出來(lái)的數(shù)據(jù)本身而不需要 care 各種異常情況

  • File的一些其它有用的擴(kuò)展函數(shù)
/** * 將文件里的所有數(shù)據(jù)以字節(jié)數(shù)組的形式讀出 * Tip:顯然這不適用于大文件,文件過(guò)大,會(huì)導(dǎo)致創(chuàng)建一個(gè)超大數(shù)組 */ public fun File.readBytes(): ByteArray/** * 與上一個(gè)函數(shù)類(lèi)似,不過(guò)這個(gè)是寫(xiě)(如果文件存在,則覆蓋) */ public fun File.writeBytes(array: ByteArray)Unit/** * 將array數(shù)組中的數(shù)據(jù)添加到文件里(如果文件存在則在文件尾部添加) */ 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

上面的函數(shù)都是基于use實(shí)現(xiàn)的,可以放心使用,而不用擔(dān)心異常的發(fā)生,并且會(huì)自動(dòng)關(guān)閉IO流

總結(jié)

以上是生活随笔為你收集整理的Kotlin实战指南十九:use 函数魔法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。