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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Kotlin实战指南十九:use 函数魔法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/117366756
本文出自【趙彥軍的博客】

文章目錄

  • 往期精彩文章
  • use函數

往期精彩文章

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流

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。