Android持久化之文件的读取与写入
16lz
2021-12-04
1、文件存储
(1)在Android的持久化技术中,文件存储是最基本的一种数据存储方式。
(2)对存储的内容部做任何处理,原样存储到文件中。
(3)Context提供了文件写入与读取的方法,openFileOutput:写入到文件;openFileInput:从文件中读取。
(4)文件写入时模式有多种:比如是覆盖写入还是追加写入等。
(5)写入的文件默认存储在/data/data/报名/files/目录下。
2、示例
在这里设置一个简单的应用场景:当在文本框中输入内容时,当下次再进入时显示上次输入的内容。
(1)activity_main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Account:" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > </EditText></LinearLayout>在该布局中,有一TextView和一输入框。
(2)MainActivity.java
package com.example.testfilestore;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.text.TextUtils;import android.view.Menu;import android.widget.EditText;/** * 文件存储:写入与读取 * @author yy * */public class MainActivity extends Activity {private EditText editText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取editText对象editText = (EditText) findViewById(R.id.editText1);//首先加载上次输入的内容String inputContent = readFromFile();if(!TextUtils.isEmpty(inputContent)){//如果上次输入的内容不为空,则加载进来editText.setText(inputContent);<span style="white-space:pre"></span>//设置光标位置,使之位于文本末尾,默认是在文本头部<span style="white-space:pre"></span>editText.setSelection(inputContent.length());}}/** * 当活动销毁时,保存输入的内容 */@Overrideprotected void onDestroy() {super.onDestroy();//获取输入的内容String data = editText.getText().toString();//写入文件writeToFile(data);}/** * 输入的内容写入文件 */public void writeToFile(String data){FileOutputStream fileOutputStream = null;BufferedWriter bufferedWriter = null;try {//Context中的方法,用于存储数据//第一个参数是文件名//第二个参数是写入模式,表示覆盖写入,如果原来有内容,则会覆盖fileOutputStream = openFileOutput("first",Context.MODE_PRIVATE);bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));bufferedWriter.write(data);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//关闭流try {if(bufferedWriter!=null){bufferedWriter.close();}} catch (IOException e) {e.printStackTrace();}}}/** * 从文件中读取数据 * @return */public String readFromFile(){FileInputStream fileInputStream = null;BufferedReader bufferedReader = null;StringBuffer stringBuffer = new StringBuffer();try {fileInputStream = openFileInput("first");bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));String line = "";while((line = bufferedReader.readLine())!=null){stringBuffer.append(line);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//关闭流try {if (bufferedReader != null) {bufferedReader.close();}} catch (IOException e) {e.printStackTrace();}}//返回return stringBuffer.toString();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}在该类中,提供了两个方法:输入内容写入文件以及从文件中加载上次输入的内容。在这两个方法中分别调用Context提供的方法openFileOutput和openFileInput。
当写入时只有文件名,当然可以添加后缀,没有路径,是默认存储的。
文件的存储时在活动销毁时进行的。
文件内容的加载是在活动创建时进行的。
3、结果
当再次进入时,会加载上次输入的内容,并且光标位于末尾。
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android关键资源详解
- Android使用本地svg及不显示问题解决
- android recovery模式及ROM制作
- 想抢先体验Android操作系统的魅力吗?那就使用Android(安卓)LiveCD
- 如何构建Android(安卓)1.5系统映像
- Android中采用Pull解析器对XML文件进行解析
- Android的源代码结构 && 一些常用术语 && Android的支持库 && ap