【Android进阶】如何使用文件来保存程序中的数据

在程序中,有很多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作


我直接写了一个帮助类,进行文件的写入和读取操作


/**
 * 用于在文件中保存程序数据
 * 
 * @author zhaokaiqiang
 * 
 */
public class FileHelper {

	private static final String TAG = "FileHelper";
	private Context mContext;

	FileHelper(Context _mContext) {
		mContext = _mContext;
	}

	// 在手机本地硬盘中保存信息
	public void save(String fileName, String content) {

		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = mContext.openFileOutput(fileName,
					Context.MODE_PRIVATE);
			fileOutputStream.write(content.getBytes());

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {

				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 读取手机硬盘中保存的文件
	public void read(String fileName) {
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = mContext.openFileInput(fileName);
			int len = 0;
			byte[] buffer = new byte[1024];
			ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();
			while ((len = fileInputStream.read(buffer)) != -1) {
				byteArrayInputStream.write(buffer, 0, len);
			}
			String string = new String(byteArrayInputStream.toByteArray());
			Log.d(TAG, string);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

注意:使用写入操作的时候,写入的内容会将上次写入的内容进行覆盖


写入的文件保存在/data/data/package name/files目录下,使用DDMS可以进行查看

如下图所示:


使用DDMS将文件导出,即可查看内容


上面这些是将数据写入到我们的手机自带的存储空间里,如果想写入我们的SDCard,那么应该怎么做呢?

下面的写入到SDCard的操作


// save infomation in the SDCard
	public boolean saveToSDCard(String fileName, String content) {

		// judge weather the SDCard exits,and can be read and written
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return false;
		}

		FileOutputStream fileOutputStream = null;
		File file = new File(Environment.getExternalStorageDirectory(),
				fileName);
		try {
			fileOutputStream = new FileOutputStream(file);
			fileOutputStream.write(content.getBytes());
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {

				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

下面是读取位于SDCard根目录下文件的操作方法

// read the file in the SDCard
	public String readFromSD(String fileName) {
		FileInputStream fileInputStream = null;
		File file = new File(Environment.getExternalStorageDirectory(),
				fileName);
		try {
			fileInputStream = new FileInputStream(file);
			int len = 0;
			byte[] buffer = new byte[1024];
			ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();
			while ((len = fileInputStream.read(buffer)) != -1) {
				byteArrayInputStream.write(buffer, 0, len);
			}
			String string = new String(byteArrayInputStream.toByteArray());
			return string;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return null;
	}




  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值