Android入门:实现一个File存储的辅助类

本文介绍了一种在Android环境中进行文件操作的方法,包括如何在内存和SD卡上保存及读取文本文件。提供了具体的Java代码实现,展示了如何使用FileOutputStream和FileInputStream来完成这些任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

File文件存储博客链接:http://blog.youkuaiyun.com/xiazdong/article/details/7687439

package com.xiazdong.file.util;

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

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

public class FileUtil {
	/**
	 * 保存文本到内存
	 * @param context
	 * @param filename
	 * @param content
	 * @param mode
	 * @throws Exception
	 */
	public static void saveTextInMemory(Context context,String filename,String content,int mode) throws Exception{
		try{
			FileOutputStream out = context.openFileOutput(filename, mode);
			out.write(content.getBytes("UTF-8"));
			out.close();
		}
		catch(Exception e){
			throw new Exception();
		}
	}
	/**
	 * 保存文件到sdcard
	 * @param filename
	 * @param content
	 * @throws Exception
	 */
	public static void saveTextInSdcard(String filename,String content) throws Exception{
		try{
			File f = new File(Environment.getExternalStorageDirectory(),filename);
			FileOutputStream out = new FileOutputStream(f);
			out.write(content.getBytes("UTF-8"));
			out.close();
		}
		catch(Exception e){
			throw new Exception();
		}
	}
	/**
	 * 从内存读取文件
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public static String loadTextFromSdcard(String filename) throws Exception{
		try{
			File f = new File(Environment.getExternalStorageDirectory(),filename);
			FileInputStream in = new FileInputStream(f);
			byte[]data = read2byte(in);
			return new String(data,"UTF-8");
		}
		catch(Exception e){
			throw new Exception();
		}
	}
	/**
	 * 从sdcard读取文件 
	 * @param context
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public static String loadTextFromMemory(Context context,String filename) throws Exception{
		try{
			FileInputStream in = context.openFileInput(filename);
			byte[]data = read2byte(in);
			return new String(data,"UTF-8");
		}
		catch(Exception e){
			throw new Exception();
		}
	}
	private static byte[] read2byte(InputStream in) throws IOException {
		byte[] data;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		byte[]buf = new byte[1024];
		int len = 0;
		while((len = in.read(buf))!=-1){
			bout.write(buf, 0, len);
		}
		data = bout.toByteArray();
		return data;
	}
}

测试代码:

FileUtil.saveTextInSdcard("1.txt","hello");		//将"hello"保存到/mnt/sdcard/1.txt中
String content = FileUtil.loadTextFromSdcard("1.txt");	//读取/mnt/sdcard/1.txt内容
FileUtil.saveTextInMemory(MainActivity.this,"1.txt","hello", Context.MODE_PRIVATE);	//将hello保存到/data/data/package/files/1.txt中
String content = FileUtil.loadTextFromMemory(MainActivity.this, "1.txt");	//读取/data/data/package/files/1.txt内容


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值