Android开发中将String数据写入本地文件

在开发中,我们经常会遇到将字符串写入文件中,用来验证我们获取的字符串是否正确,下面我们用讲解一个简单的写入文件的方法:

首先,我们得先添加写入的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

下面是对应的写入代码:本篇主要是Kotlin写的:

    @JvmStatic
        fun writeTxtToFile(strcontent: String, filePath: String, fileName: String) {

        // 将字符串写入到文本文件中
//        fun writeTxtToFile(strcontent: String, filePath: String, fileName: String) {
            //生成文件夹之后,再生成文件,不然会出错
            makeFilePath1(filePath, fileName)
            val strFilePath = filePath + fileName
            // 每次写入时,都换行写
            val strContent = """
           $strcontent
           
           """.trimIndent()
            try {
                val file = File(strFilePath)
                if (!file.exists()) {
                    Log.d("TestFile", "Create the file:$strFilePath")
                    file.parentFile.mkdirs()
                    file.createNewFile()
                }
                val raf = RandomAccessFile(file, "rwd")
                raf.seek(file.length())
                raf.write(strContent.toByteArray())
                raf.close()
            } catch (e: java.lang.Exception) {
                Log.e("TestFile", "Error on write File:$e")
            }
        }

        //生成文件
        fun makeFilePath1(filePath: String, fileName: String): File? {
            var file: File? = null
            makeRootDirectory1(filePath)
            try {
                file = File(filePath + fileName)
                if (!file.exists()) {
                    file.createNewFile()
                }
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
            return file
        }

        //生成文件夹
        fun makeRootDirectory1(filePath: String?) {
            var file: File? = null
            try {
                file = File(filePath)
                if (!file.exists()) {
                    file.mkdir()
                }
            } catch (e: java.lang.Exception) {
                Log.i("error:", e.toString() + "")
            }
        }

相关推荐

  1. Android开发String数据写入本地文件

    2023-12-15 14:44:05       66 阅读
  2. 【C语言】如何数据写入文件

    2023-12-15 14:44:05       45 阅读
  3. 使用MATLAB的`xlswrite`函数数据写入Excel文件

    2023-12-15 14:44:05       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 14:44:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 14:44:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 14:44:05       82 阅读
  4. Python语言-面向对象

    2023-12-15 14:44:05       91 阅读

热门阅读

  1. IIS配置多域名跨域

    2023-12-15 14:44:05       97 阅读
  2. Ruoyi-vue项目中用户列表分页数据错乱的解决办法

    2023-12-15 14:44:05       55 阅读
  3. 【刷题笔记1】

    2023-12-15 14:44:05       57 阅读
  4. 【Python 千题 —— 基础篇】字符串拼接

    2023-12-15 14:44:05       58 阅读
  5. 天勤模拟卷二

    2023-12-15 14:44:05       51 阅读
  6. Golang中使用errors返回调用堆栈信息

    2023-12-15 14:44:05       58 阅读
  7. flutter 个人用户信息有多个接口拼接成的

    2023-12-15 14:44:05       59 阅读