java/kotlin 读取文件、写入文件

package dh.btb.backend.utils
import java.io.*object FileUtil {

    /**
     * 创建文件
     * @param filePath 文件路径(不要以/结尾)
     * @param fileName 文件名称(包含后缀,如:ReadMe.txt)
     * @throws IOException
     */
    @Throws(IOException::class)
    fun createTxtFile(filePath: String, fileName: String): Boolean {
        var flag = false
        val filename = File("$filePath/$fileName")
        if (!filename.exists()) {
            filename.createNewFile()
            flag = true
        }
        return flag
    }

    /**
     * 写文件
     *
     * @param content 文件内容
     * @param filePath 文件路径(不要以/结尾)
     * @param fileName 文件名称(包含后缀,如:ReadMe.txt)
     * 新内容
     * @throws IOException
     */
    fun writeTxtFile(content: String, filePath: String, fileName: String, append: Boolean): Boolean {
        var flag: Boolean = true
        val thisFile = File("$filePath/$fileName")
        try {
            if (!thisFile.parentFile.exists()) {
                thisFile.parentFile.mkdirs()
            }
            val fw = FileWriter("$filePath/$fileName", append)
            fw.write(content)
            fw.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return flag
    }

    /**
     * 读TXT文件内容
     * @param filePath 文件路径(不要以 / 结尾)
     * @param fileName 文件名称(包含后缀,如:ReadMe.txt)
     * @return
     */
    @Throws(Exception::class)
    fun readTxtFile(filePath: String, fileName: String): String? {
        var result: String? = ""
        val fileName = File("$filePath/$fileName")
        var fileReader: FileReader? = null
        var bufferedReader: BufferedReader? = null
        try {
            fileReader = FileReader(fileName)
            bufferedReader = BufferedReader(fileReader)
            try {
                var read: String? = null
                while ({ read = bufferedReader.readLine();read }() != null) {
                    result = result + read + "
"
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close()
            }
            if (fileReader != null) {
                fileReader.close()
            }
        }
        println("读取出来的文件内容是:
$result")
        return result
    }
}

fun main(args: Array<String>) {
    val service = FileUtil
    val pathName = "E:/temp"
    val fileName = "ReadMe.json"
    val content = "我现在在上班" +
            "比较忙的时候别来打扰我"
    service.createTxtFile(pathName, fileName)
    service.writeTxtFile(content, pathName, fileName, false)
    val str = service.readTxtFile(pathName, fileName)
    println(str)
}
原文地址:https://www.cnblogs.com/dwb91/p/9049537.html