Kotlin總結(jié)3

Date

這個是日常用的比較多的類,在kotlin中用傳統(tǒng)的方法,IDEA會提示語法警告,有更好的方法,就是下面的

  • 傳統(tǒng)的方法
    fun formatDate(date: Date, pattern: String): String = SimpleDateFormat(pattern).format(date)
  • kotlin推薦方法
    fun formatDate(date: Date, dateFormat: DateFormat): String = dateFormat.format(date)

  • 提示如下

To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale. US for ASCII dates.

獲得本地格式化請使用getDateInstance(),getDateTimeInstance(),或者getTimeInstance(),或者使用new SimpleDateFormat(String template, Locale locale),例如Locale.US為ASCII日期。

  • 但是它只提供了常用的,這些之外的,還是自定義吧

靜態(tài)常量和靜態(tài)方法共存

  • 工具類

object DateUtils {
    
    const val SFStr = "yyyyMMdd"

    fun formatDate(date: Date, pattern: String): String = SimpleDateFormat(pattern).format(date)
    }

  • 引用
//java
DateUtils.INSTANCE.formatDate(new Date(),DateUtils.DF_YYYYMMDDHHMMSS);
//kotlin
DateUtils.formatDate(Date(),DateUtils.DF_YYYYMMDDHHMMSS)

java的引用,看起來是個單例,但是我的習(xí)慣是像java的靜態(tài)方法一樣調(diào)用

  • class可以做到,如果把工具類改成class,那么靜態(tài)常量就沒法用了
  • 伴生對象也可以做到,但object又不允許有伴生對象
  • 所以,加個注解就搞定了@JvmStatic
  • 加了注解不影響kotlin調(diào)用,只是簡化了java調(diào)用
//工具類方法
@JvmStatic
fun formatDate(date: Date, pattern: String): String = SimpleDateFormat(pattern).format(date)

//java調(diào)用
DateUtils.formatDate(new Date(),DateUtils.DF_YYYYMMDDHHMMSS);

Kotlin讀寫流操作

寫文件在java中是這么操作的

    public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
        InputStream in = new ByteArrayInputStream(bytes);
        File destFile = new File(filePath);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        destFile.createNewFile();
        OutputStream out = new FileOutputStream(destFile);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = in.read(cache)) != -1) {
            out.write(cache, 0, nRead);
            out.flush();
        }
        out.close();
        in.close();
    }

轉(zhuǎn)成kotlin后,是不允許在while中寫賦值表達(dá)式的,弄好好久,發(fā)現(xiàn)應(yīng)該是這樣的

    @Throws(Exception::class)
    fun byteArrayToFile(bytes: ByteArray, filePath: String) {
        val inStream = ByteArrayInputStream(bytes)
        val destFile = File(filePath)
        if (!destFile.parentFile.exists()) {
            destFile.parentFile.mkdirs()
        }
        destFile.createNewFile()
        val out = FileOutputStream(destFile)
        val cache = ByteArray(CACHE_SIZE)
        var nRead = inStream.read(cache)

        while (nRead != -1) {
            out.write(cache, 0, nRead)
            nRead = inStream.read(cache)
        }
        inStream.copyTo(out)
        out.close()
        inStream.close()
    }

然后Slient大神發(fā)了一個擴(kuò)展方法InputStream.copyTo

于是就變成這樣了

@Throws(Exception::class)
fun byteArrayToFile(bytes: ByteArray, filePath: String) {
    val inStream = ByteArrayInputStream(bytes)
    val destFile = File(filePath)
    if (!destFile.parentFile.exists())
        destFile.parentFile.mkdirs()
    destFile.createNewFile()
    val out = FileOutputStream(destFile)
    inStream.copyTo(out,MemoryUtils.KB)
    out.close()
    inStream.close()
}

臥槽,感覺好多語法糖,上次忘了一個什么方法,寫了半天,也是Slient大神給了個語法糖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容