Kotlin Files Paths write ByteArray writeString写多行BufferedWriter

Kotlin Files Paths write ByteArray writeString写多行BufferedWriter

Kotlin 复制代码
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardOpenOption

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

    val bytes: ByteArray = "hello,".toByteArray() //覆盖写文件
    Files.write(path, bytes)

    val string: String = "world!"
    Files.writeString(path, string, StandardOpenOption.APPEND) //原文件追加

    val bufferedWriter = Files.newBufferedWriter(path, StandardOpenOption.APPEND)
    bufferedWriter.newLine() //换行
    bufferedWriter.flush()

    var lines = mutableListOf<String>()
    repeat(3) {
        lines.add(it.toString())
    }
    Files.write(path, lines, StandardOpenOption.APPEND) //写入多行

    bufferedWriter.write("2023")
    bufferedWriter.flush() //预备写的数据在内存,flush流,写入本地。
    bufferedWriter.close()

    //打印文件全部内容
    println(Files.readString(path))
}

hello,world!

0

1

2

2023

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

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

相关推荐
雨白1 小时前
深入理解 Kotlin 协程 (四):大道至简,于微末的挂起恢复中衍化万物
kotlin
jinanwuhuaguo9 小时前
OpenClaw 2026年4月升级大系深度解读剖析:从“架构重塑”到“信任内建”的范式跃迁
android·开发语言·人工智能·架构·kotlin·openclaw
我命由我1234511 小时前
Android Jetpack Compose - SearchBar(搜索栏)、Tab(标签页)、时间选择器、TooltipBox(工具提示)
android·java·java-ee·kotlin·android studio·android jetpack·android-studio
Lsk_Smion11 小时前
Hot100(开刷) 之 长度最小的数组--删除倒数第N个链表--层序遍历
java·数据结构·算法·kotlin
我命由我1234513 小时前
Android Jetpack Compose - 组件分类:布局组件、交互组件、文本组件
android·java·java-ee·kotlin·android studio·android jetpack·android-studio
Kapaseker14 小时前
让你的 App 成为 AI 的一环
android·kotlin
千码君20161 天前
kotlin:Jetpack Compose 给APP添加声音(点击音效/背景音乐)
android·开发语言·kotlin·音效·jetpack compose
Kapaseker2 天前
lazy 与 lateinit 到底有什么区别?
android·kotlin
雨白2 天前
深入理解 Kotlin 协程 (三):返璞归真,探寻协程基础设施的底层基石
kotlin
call me by ur name2 天前
ERNIE 5.0 Technical Report论文解读
android·开发语言·人工智能·机器学习·ai·kotlin