Kotlin File writeText appendText appendBytes readBytes readText

Kotlin File writeText appendText appendBytes readBytes readText

Kotlin 复制代码
import java.io.File

fun main(args: Array<String>) {
    val filePath = "./myfile.txt"

    val file = File(filePath)

    file.writeText("hello,") //如果原有文件有内容,将完全覆盖。
    file.appendText("world!") //在原文件尾部追加。
    file.appendBytes("2023 year".toByteArray()) //在原文件尾部追加。

    val bytes: ByteArray = file.readBytes()
    println(String(bytes))

    println(file.readText())
}

Kotlin Files readAllBytes readAllLines readString-CSDN博客Java把一个文件转化为byte字节数组 /** * 把一个文件转化为byte字节数组。try { File..._java文件转byte数组。Java把一个文件转化为byte字节数组_java文件转byte数组_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/132763802

相关推荐
赏金术士2 小时前
Kotlin 数据流与单双向绑定
android·开发语言·kotlin
帅次9 小时前
Modifier 链与顺序、测量与命中区域
android·kotlin·compose·modifier
小仙女喂得猪11 小时前
2026 Android 组件化项目的AICoding落地实践
android·kotlin·ai编程
帅次14 小时前
状态 StateFlow、ViewModel 与 UI 收集
android·kotlin·gradle·android studio·android jetpack
Kapaseker16 小时前
Android 中的 MVVM 是如何构建起来的
android·kotlin
雨白1 天前
深入理解 Kotlin 协程 (六):进退有度,解密协程取消响应与异常分发机制
kotlin
百锦再1 天前
Auto.js变成基础知识学习
开发语言·javascript·学习·sqlite·kotlin·android studio·数据库开发
帅次1 天前
Compose 入门:@Composable、组合与重组
android·kotlin·gradle·android jetpack·compose·composable
Junerver1 天前
Kotlin - 约定contract
kotlin
Junerver1 天前
使用datetime更加优雅地在kotlin中处理时间
kotlin