前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android数据存储方式之文件存储

Android数据存储方式之文件存储

原创
作者头像
泰坦HW
修改2021-09-10 11:33:13
10.6K0
修改2021-09-10 11:33:13
举报
文章被收录于专栏:Titan笔记Titan笔记

文件存储是Android中数据存储的基本方式之一,Android提供了openFileOutputopenFileInput两个方法来提供FileOutStreamFileInputStream,文件将会存储在APP的数据目录中(一般是/data/data/APP包名)。

下面是一个简单的示例

FileStorageActivity的布局文件如下:

代码语言:html
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp"
    tools:context=".FileStorageActivity">
    <EditText
        android:id="@+id/fileEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp" />
    <Button
        android:id="@+id/fileWriteButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="写入数据" />
    <Button
        android:id="@+id/fileReadButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取数据" />
</LinearLayout>

FileStorageActivity

代码语言:java
复制
package cn.titan6.data.storage.demo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import java.io.*
class FileStorageActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_file_storage)
        val editText: EditText = findViewById(R.id.fileEditText)
        val writeButton: Button = findViewById(R.id.fileWriteButton)
        val readButton: Button = findViewById(R.id.fileReadButton)
        writeButton.setOnClickListener {
            saveFileData(editText.text.toString())
            Log.i("FileStorageActivity", "Save data finished")
            editText.setText("")
        }
        readButton.setOnClickListener {
            val str = readFileData()
            Log.i("FileStorageActivity", "Read data finished")
            editText.setText(str)
        }
    }
    private fun saveFileData(str: String) {
        val openFileOutput = openFileOutput("data", MODE_PRIVATE)
        val writer = BufferedWriter(OutputStreamWriter(openFileOutput))
        writer.use {
            it.write(str)
        }
    }
    private fun readFileData(): String {
        val content = StringBuilder()
        val input = openFileInput("data")
        val reader = BufferedReader(InputStreamReader(input))
        reader.use {
            reader.forEachLine {
                content.append(it)
            }
        }
        return content.toString()
    }
}

本文为本人原创文章,同步发布于Titan笔记

https://www.titan6.cn/archives/343.html

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • FileStorageActivity的布局文件如下:
  • FileStorageActivity
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档