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

相关推荐
Tom4i1 小时前
Kotlin 中的 inline 和 reified 关键字
android·开发语言·kotlin
无知的前端4 小时前
一文读懂-Jetpack与AndroidX
android·kotlin·android jetpack
用户69371750013849 小时前
Kotlin 函数详解:命名参数与默认参数值
android·后端·kotlin
Jeled11 小时前
RecyclerView ViewHolder 复用机制详解(含常见错乱问题与优化方案)
android·学习·面试·kotlin
柯南二号13 小时前
【大前端】【Android】 Kotlin 语法超详细解析(2025 最新)
android·kotlin
Ya-Jun1 天前
项目实战Now in Android:项目模块说明
android·架构·kotlin
消失的旧时光-19431 天前
Kotlinx.serialization 使用指南
android·kotlin·json
消失的旧时光-19431 天前
Kotlinx.serialization 项目集成
android·kotlin·json
彭同学学习日志1 天前
解决 Android Navigation 组件导航栏配置崩溃:从错误到实现的完整指南
android·kotlin
怪兽20142 天前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin