Kotlin Files readAllBytes readAllLines readString

Kotlin Files readAllBytes readAllLines readString

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

fun main(args: Array) {
    val filePath = "./myfile.txt"
    
    val path: Path = Paths.get(filePath)
    val bytes: ByteArray = Files.readAllBytes(path)
    val content = String(bytes, StandardCharsets.UTF_8)
    
    println(content)
}

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

fun main(args: Array) {
    val filePath = "./myfile.txt"
    val path: Path = Paths.get(filePath)
    val lines = Files.readAllLines(path)
    lines.forEach {
        println(it)
    }
}

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

fun main(args: Array) {
    val filePath = "./myfile.txt"
    val path: Path = Paths.get(filePath)
    val string = Files.readString(path, StandardCharsets.UTF_8)
    println(string)
}

Java把一个文件转化为byte字节数组_java文件转byte数组_zhangphil的博客-CSDN博客Java把一个文件转化为byte字节数组 /** * 把一个文件转化为byte字节数组。 * * @return */ private byte[] fileConvertToByteArray(File file) { byte[] data = null; try { File..._java文件转byte数组https://blog.csdn.net/zhangphil/article/details/83115640

你可能感兴趣的:(kotlin,kotlin)