您的位置:首页 > 移动开发 > Android开发

android保存到手机内存【数据存储】

2015-12-24 11:09 513 查看
小文件,像应用的配置信息等保存到手机内存就行

使用文件进行数据存储

Context.MODE_PRIVATE私有操作模式:文件仅能被本应用访问,数据采用覆盖的方式写入文件

Context.MODE_APPEND追加操作模式:文件仅能被本应用访问,数据采用追加的方式写入文件

Context.MODE_WORLD_READABLE:文件可以被其它应用读取

Context.MODE_WORLD_WRITEABLE:文件可以被其它应用写入

android 有一套自己的安全模型,当应用程序(.apk)在安装

时系统会分配给他一个userid,当该应用要去访问其它应用资源就需要userid匹配,默认情况下,任何应用创建的文件,sharedpreferences,数据库都应该是私有的(位于/data/data/<package name>/files),其它应用程序无法访问

除非在创建时指定了Context.MODE_WORLD或者Context.MODE_WORLD_WRITEABLE。

读取文件内容

如果要打开存放在/data/data/<package name>/files目录应用私有的文件,可以使用

Activity提供openFileInput()方法

FileInputStream inStream = this.getContext().openFileinput("文件名");

Log.i("FileTest",readinStream(inStream));

或者使用文件的绝对路径

File file = new File("/data/data/<package name>/files/文件名");

FileInputStream inStream = new FileInputStream(file);

<package name>要换成应用所在包

package cn.itcast.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.Context;
import android.os.Environment;

public class FileService {
	/*
	 * 文件保存属于业务层,根据MVC模式的思想
	 * 这里写了一个专门的FileSarvie类处理文件保存的操作
	 * */
	private Context context;

	public FileService(Context context) {
		this.context = context;
	}

	

	/**
	 * 保存文件  主要使用java的IO流操作
	 * @param filename 文件名称
	 * @param context 文件内容
	 * 使用context.openFileOutput方法保存数据默认情况
	 * 应用所产生的数据会保存到data/date下的对应包下会创建一个files文件夹,然后把文件保存files下
	 * 
	 */
	public void save(String filename, String content) throws Exception {
		//私有操作模式:创建出来的文件只能被本应用访问,其它应用无法访问该文件,另外采用私有操作模式创建的文件,写入文件

中的内容会覆盖原文件的内容

		/**
		 * 提供文件名和文件内容进行保存
		 * 创建输出流的方法有很多,android系统 提供了一个非常方便的方法--上下文对象,里面有一个方法可以快速得到一个文件输

出流对象
		 */
		FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
		outStream.write(content.getBytes());
		outStream.close();
	}
	/**
	 * 文件读取:
	 * 读取文件内容,使用context提供的方法。文件输入流(输入和输出是相对于android程序而言的,不是相对于文件而言)
	 * context.openFileInput(filename)默认是在data/data/对应的程序包下的files文件夹下查找文件
	 * @param filename 文件名称
	 * @return 文件内容
	 * @throws Exception
	 */
	public String read(String filename) throws Exception {
		FileInputStream inStream = context.openFileInput(filename);
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while((len = inStream.read(buffer)) != -1){
			outStream.write(buffer, 0, len);//使用输出流对象,把每次读到的数据先写到内存中
		}
		byte[] data = outStream.toByteArray();//获取存到内存中的所有数据
		return new String(data);
	}

	/**
	 * 保存文件
	 * @param filename 文件名称
	 * @param context 文件内容
	 */
	public void saveAppend(String filename, String content) throws Exception {//ctrl+shift+y / x
		//追加操作模式:创建出来的文件只能被本应用访问,其它应用无法访问该文件,写入文件中的内容会追加到原文件的内容的后

面
		FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);
		outStream.write(content.getBytes());
		outStream.close();
	}

	/**
	 * 保存文件
	 * @param filename 文件名称
	 * @param content 文件内容
	 */
	public void saveReadable(String filename, String content) throws Exception {
		//MODE_WORLD_READABLE操作模式创建出来的文件可以被其他应用读取
		FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
		outStream.write(content.getBytes());
		outStream.close();
	}

	/**
	 * 保存文件
	 * @param filename 文件名称
	 * @param content 文件内容
	 */
	public void saveWriteable(String filename, String content) throws Exception {
		//MODE_WORLD_WRITEABLE:创建出来的文件只能可以被其它应用写入
		FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE);
		outStream.write(content.getBytes());
		outStream.close();
	}

	/**
	 * 保存文件
	 * @param filename 文件名称
	 * @param content 文件内容
	 */
	public void saveRW(String filename, String content) throws Exception {
		//这样创建出来的文件可以被其它应用写入和读取
		FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE + 

Context.MODE_WORLD_READABLE);
		outStream.write(content.getBytes());
		outStream.close();
	}

}


单元测试类

package cn.itcast.test;

import cn.itcast.service.FileService;
import android.test.AndroidTestCase;
import android.util.Log;

public class FileServiceTest extends AndroidTestCase {
	/**
	 * 进行单元测试的类里面写的是 单元测试用例
	 * 
	 */
	private static final String TAG = "FileServiceTest";
	public void testsaved() throws Exception{
		FileService service = new FileService(this.getContext());
		service.save("itcast.txt","iloveyou");
	}

	public void testRead() throws Throwable{
		FileService service = new FileService(this.getContext());
		String result = service.read("itcast.txt");
		Log.i(TAG, result);
	}

	public void testSaveAppend() throws Throwable{
		FileService service = new FileService(this.getContext());
		service.saveAppend("append.txt", ",liming");
	}

	public void testSaveReadable() throws Throwable{
		FileService service = new FileService(this.getContext());
		service.saveReadable("readable.txt", "readable");
	}

	public void testSaveWriteable() throws Throwable{
		FileService service = new FileService(this.getContext());
		service.saveWriteable("writeable.txt", "writeable");
	}

	public void testSaveRW() throws Throwable{
		FileService service = new FileService(this.getContext());
		service.saveRW("rw.txt", "rw");
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: