一、持久化技术

  • 我们平时所使用的APP产生的数据,在内存中都是瞬时的,会随着断电、关机等丢失数据,因此android系统采用了持久化技术,用于存储这些“瞬时”数据
  • 持久化技术包括:文件存储、SharedPreference存储以及数据库存储,还有更复杂的SD卡内存储。

二、文件存储

  • 最基本存储方式,不对存储内容进行格式化处理,适用于存储简单文本或者二进制数据,若存储一些复杂数据,那么需要定义格式规范,方便后续解析出来。
  • Context类提供了openFileOutput方法用于将数据存储到文件中。
  • 该方法接收两个参数,第一个为文件名(不可以包含路径,因为数据已经默认存储到/data/data<packagename>/files/目录下面了。第二个参数时文件的操作模式,包括两种MODE_PRIVATEMODE_APPEND,前一个是默认,缩写内容会覆盖。后一个表示追加内容,不存在就创建文件。
  • openFileOutput()方法返回的是一个FileOutputStream对象,得到该对象之后,可以使用流的方式写入文件。以下是一段简单的演示:
public void save(){
	String data = "Data to save";
	FileOutputStream out = null;
	BufferedWriter writer = null;
	try{
		out = openFileOutput("data",Context.MODE_PRIVATE);
		writer = new BufferedWrirter(new OutputStreamWriter(out));
		writer.write(data);
	} catch (IOException e){
		e.printStackTrace();
	} finally {
		try{
			if(writer != null){
				writer.close();
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
  • 如上就是写一个存储文件的Java流的一系列操作,如果不明白其中的含义,可以参见Java连载的流的那几期。

1.建立一个FilePersistenceTest项目用于演示

  • 先修改anctivity_main.xml文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
	
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type something here" />
       
</LinearLayout>
  • 运行显示:
    29.1
  • 可以看到APP有一个地方可以写入文本,但是这都是临时文件,一退出就没了,因此我们需要改一下这个主xml的活动逻辑MainActivity.java文件
package com.example.filepersistencetest;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	private EditText edit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		edit = (EditText)findViewById(R.id.edit);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		String inputText = edit.getText().toString();
		save(inputText);
	}
	
	public void save(String inputText) {
		FileOutputStream out = null;
		BufferedWriter writer = null;
		try{
			out = openFileOutput("data",Context.MODE_PRIVATE);
			writer = new BufferedWriter(new OutputStreamWriter(out));
			writer.write(inputText);
		} catch (IOException e){
			e.printStackTrace();
		} finally {
			try{
				if(writer != null){
					writer.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
}

  • 解析:我们在主活动中获取了EditText标签的文本内容,保存在了edit变量中,然后重写了onDestroy()方法,保证了在销毁这个活动的时候,能够保证内容已经保存在APP中,save方法基本和我们之前举例的一致。

  • 那么如果确认我们已经保存了数据呢?
    29.2

  • 打开DDMS的File Explorer来查看一下,按图示打开文件com.example.persistencetest/files/目录下面内容,并且到处,使用notepad++打开看一看,确实是我们之前输入的
    29.3

2.如何从文件中读取数据

  • 读取文件的函数openFileInput()方法,用于从文件中读取数据,参数只有一个即使文件名,当然也不需要路径,因为android已经提前定义好了/data/data/<packagename>/files/目录下面,并返回一个FileInputStream对象,演示一波
public String load(){
	FileInputStream in = null;
	BufferReader reader = null;
	StringBuilder content = new StringBuilder();
	try {
		in = openFileInput("data");
		reader = new BufferedReader(new InputStreamReader(in));
		String line = "";
		while((line=reader.readLine())!=null){
			content.append(line);
		}
	}catch(IOException e){
		e.printStackTrace();
	}finally{
		if(reader != null){
			try{
				reader.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
	return content.toString();
}
  • 上面的代码显示出一行一行的读取出文件内容。

三、源码:

posted on 2020-08-13 00:07  心悦君兮君不知-睿  阅读(279)  评论(0编辑  收藏  举报