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

相关推荐
alexhilton4 小时前
Jetpack Compose中的富文本输入
android·kotlin·android jetpack
进击的cc10 小时前
Android Kotlin:扩展函数如何优雅封装Android API
android·kotlin
进击的cc10 小时前
Android Kotlin:空安全机制在Android中的实战应用
android·kotlin
海盐芝士不加糖14 小时前
我又又又辞职了,然后做了一款“离线版微信”
kotlin·app·android jetpack
Kapaseker15 小时前
你知道屏幕上存在多少个 Window 吗
android·kotlin
stevenzqzq2 天前
Kotlin 进阶指南:中缀函数 (Infix Function)
android·kotlin·compose
Kapaseker2 天前
Android Studio 是如何预览 Compose 的
android·kotlin
__Yvan2 天前
Kotlin 的 ?.let{} ?: run{} 真的等价于 if-else 吗?
android·开发语言·前端·kotlin
tangweiguo030519872 天前
Android WorkManager 完整实战教程(含完整文件)
android·kotlin
顾道长生'3 天前
(Arxiv-2026)HiAR:基于分层去噪的高效自回归长视频生成
回归·kotlin·音视频·长视频生成