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

相关推荐
进击的cc4 小时前
Android Kotlin:委托属性深度解析
android·kotlin
进击的cc4 小时前
Android Kotlin:Kotlin数据类与密封类
android·kotlin
博.闻广见7 小时前
19-Compose开发-LazyColumn
kotlin·composer
糖猫猫cc9 小时前
Kite 实现逻辑删除
java·kotlin·orm·kite
UXbot9 小时前
AI App 设计生成工具哪个好?
ui·kotlin·软件构建·产品经理·ai编程·swift
simplepeng11 小时前
Kotlin 2.3 编译器:大型代码库构建速度提升 40% 以上
kotlin
Kapaseker12 小时前
Compose 官方 API 搞定文本输入格式
android·kotlin
博.闻广见12 小时前
16-Kotlin高阶特性-Lambda详解
kotlin
博.闻广见1 天前
17-Compose开发-单向数据流
kotlin·composer
Kapaseker1 天前
Kotlin 精讲 — companion object
android·kotlin