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

相关推荐
Jeled11 小时前
「高级 Android 架构师成长路线」的第 1 阶段 —— 强化体系与架构思维(Clean Architecture 实战)
android·kotlin·android studio·1024程序员节
明道源码13 小时前
Kotlin 控制流、函数、Lambda、高阶函数
android·开发语言·kotlin
橙子1991101613 小时前
在 Kotlin 中,ViewModel 的获取
开发语言·vue.js·kotlin
hweiyu0013 小时前
Gradle 构建脚本迁移:从 Groovy DSL 到 Kotlin DSL,语法与技巧对比
开发语言·kotlin·gradle
消失的旧时光-194313 小时前
搞懂 Kotlin 的 List、Set、Map、HashMap、LinkedHashMap,以及 asSequence() 的底层原理与实战场景。
kotlin·数据处理·1024程序员节
明道源码13 小时前
Kotlin 面向对象编程、主构造函数、次构造函数、伴生对象、数据类、继承
kotlin·1024程序员节
消失的旧时光-194315 小时前
Kotlin × Gson:为什么遍历 JsonObject 要用 entrySet()
android·kotlin·数据处理·1024程序员节
AH_HH1 天前
SmartCabinet:基于 Android 的智能储物柜管理系统技术解析
android·kotlin·存储柜
消失的旧时光-19432 天前
kmp需要技能
android·设计模式·kotlin
雨白2 天前
协程间的通信管道 —— Kotlin Channel 详解
android·kotlin