• Android学习第十天


    文件存储

    Context类中提供了一个openFileOutput()方法,可以用于将数据存储到指定的文件中。
    所有的文件会默认存储到/data/data/<package name>/files/目录下。示例写法如下:
    fun save(inputText: String) {
    try {
    val output = openFileOutput("data", Context.MODE_PRIVATE)
    val writer = BufferedWriter(OutputStreamWriter(output))
    writer.use {
    it.write(inputText)
    }
    } catch (e: IOException) {
    e.printStackTrace()
    }
    }

    从文件中读取数据

    Context类中还提供了一个openFileInput()方法,用于从文件中读取数据。
    它会自动到/data/data/<package name>/files/目录下加载文件,并返回一个FileInputStream对象,得到
    这个对象之后,再通过流的方式就可以将数据读取出来了。示例写法如下:
    fun load(): String {
    val content = StringBuilder()
    try {
    val input = openFileInput("data")
    val reader = BufferedReader(InputStreamReader(input))
    reader.use {
    reader.forEachLine {
    content.append(it)
    }
    }
    } catch (e: IOException) {
    e.printStackTrace()
    }
    return content.toString()
    }

    SharedPreferences存储

    不同于文件的存储方式,SharedPreferences是使用键值对的方式来存储数据的。
    也就是说,当保存一条数据的时候,需要给这条数据提供一个对应的键,这样在读取数据的时候就可
    以通过这个键把相应的值取出来。
    SharedPreferences还支持多种不同的数据类型存储,如果存储的数据类型是整型,那么读取出来的数
    据也是整型的;如果存储的数据是一个字符串,那么读取出来的数据仍然是字符串。

    将数据存储到SharedPreferences中

    向SharedPreferences文件中存储数据了,主要可以分为3步实现。
    • 调用SharedPreferences对象的edit()方法获取一个SharedPreferences.Editor对象。
    • 向SharedPreferences.Editor对象中添加数据,比如添加一个布尔型数据就使用putBoolean()方
    法,添加一个字符串则使用putString()方法,以此类推。
    • 调用apply()方法将添加的数据提交,从而完成数据存储操作。
    val editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit()
    editor.putString("name", "Tom")
    editor.putInt("age", 28)
    editor.putBoolean("married", false)
    editor.apply()

     从SharedPreferences中读取数据

    SharedPreferences对象中提供了一系列的get方法,用于对存储的数据进行读取,每种get方法都对应了
    SharedPreferences.Editor中的一种put方法。
    比如读取一个布尔型数据就使用getBoolean()方法,读取一个字符串就使用getString()方法。
    示例写法如下:
    val prefs = getSharedPreferences("data", Context.MODE_PRIVATE)
    val name = prefs.getString("name", "")
    val age = prefs.getInt("age", 0)
    val married = prefs.getBoolean("married", false)

    数据库存储

      创建数据库

      

    Android为了让我们能够更加方便地管理数据库,专门提供了一个SQLiteOpenHelper帮助类,借助这个
    类可以非常简单地对数据库进行创建和升级。示例写法如下:
    class MyDatabaseHelper(val context: Context, name: String, version: Int) : SQLiteOpenHelper(context, name, null, version) {
    private val createBook = "create table Book (" +
    "id integer primary key autoincrement," +
    "author text," +
    "price real," +
    "pages integer," +
    "name text)"
    override fun onCreate(db: SQLiteDatabase) {
    db.execSQL(createBook)
    Toast.makeText(context, "Create succeeded", Toast.LENGTH_SHORT).show()
    }
    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
    }
    }

    升级数据库

    class MyDatabaseHelper(val context: Context, name: String, version: Int) : SQLiteOpenHelper(context, name, null, version) {

    private val createCategory = "create table Category (" +
    "id integer primary key autoincrement," +
    "category_name text," +
    "category_code integer)"
    override fun onCreate(db: SQLiteDatabase) {
    db.execSQL(createBook)
    db.execSQL(createCategory)
    Toast.makeText(context, "Create succeeded", Toast.LENGTH_SHORT).show()
    }
    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
    db.execSQL("drop table if exists Book")
    db.execSQL("drop table if exists Category")
    onCreate(db)
    }
    }

  • 相关阅读:
    前端开发试题
    操作手册
    border-box有什么用
    npm安装react-dom...
    html-webpack-plugin按需加载的js/css也会被提取出来吗
    洛谷P3957 跳房子(Noip2017普及组 T4)
    【react】利用prop-types第三方库对组件的props中的变量进行类型检测
    React 进入页面以后自动 focus 到某个输入框
    React 更新阶段的生命周期 componentWillReceiveProps->shouldComponentUpdate->componentWillUpdate
    React 生命周期 constructor->componentWillMount->render->componentDidMount->componentWillUnmount
  • 原文地址:https://www.cnblogs.com/yongyuandishen/p/14867539.html
Copyright © 2020-2023  润新知