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

相关推荐
汪海游龙2 小时前
流式回复结尾的那一下闪:逐帧定位 Compose 聊天列表的三处布局跳动
android·性能优化·kotlin
传奇开心果编程3 小时前
【Compose Multiplatform跨端声明式UI实用技术】第20课:CMP跨平台经典示例深度解析
kotlin·移动开发·跨平台·编程语言理论·开发范式·编程框架·声明式ui
敲代码的瓦龙5 小时前
Jetpack?lifecycle!!!
android·kotlin
Kapaseker9 小时前
为什么程序员始终无法彻底解决内存泄漏?
android·kotlin
汤姆宝库10 小时前
推理工程深拆:调度、量化与投机解码
android·开发语言·kotlin
传奇开心果1 天前
【Compose Multiplatform跨端声明式UI实用技术】第9课:跨平台 ViewModel 与状态架构
android·kotlin
andyweike1 天前
kotlin dsl library工程怎么配置
android·开发语言·kotlin
high一马平川2 天前
# SystemUI Dagger2 精读 · 第 2 章 @Inject:1200 处构造注入,和 1 处例外
android·java·kotlin
小铁-Android2 天前
CompletableDeferred:Kotlin 协程中用于一次性挂起通信的工具
android·kotlin