Android 文件操作

一、基本概念

 Android中读取/写入文件的方法,与Java中的I/O是一样的,提供了openFileInput()和openFileOutput()方法来读取设备上的文件。但是在默认状态下,文件是不能在不同的程序之间共享的,以上两个方法只支持读取该应用目录下的文件,读取非其自身目录下的文件将会抛出FileNotFoundException异常。创建的文件存放在/data/data/<package name>/files目录下。

在Android中,对文件提供了四中操作模式:

私有模式
①只能被创建这个文件的当前应用访问
②若文件不存在会创建文件;若创建的文件已存在则会覆盖掉原来的文件
Context.MODE_PRIVATE = 0;

 

追加模式
①私有的
②若文件不存在会创建文件;若文件存在则在文件的末尾进行追加内容
Context.MODE_APPEND = 32768;

 

可读模式
①创建出来的文件可以被其他应用所读取
Context.MODE_WORLD_READABLE=1;

 

可写模式
①允许其他应用对其进行写入。
Context.MODE_WORLD_WRITEABLE=2

 

以上文件操作模式均针对保存在手机自带存储空间的文件。若文件存储在SDCard上,则不受读写控制。

二、组合使用

[java]  view plain copy print ?
  1. FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);  

允许其他应用读写,并默认覆盖

[java]  view plain copy print ?
  1. FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_APPEND+Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);  

追加模式,但允许其他应用读写


三、具体 代码


package com.kelly.filedemo.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

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

@SuppressLint("WorldReadableFiles")
public class FileService {

	private Context context;

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

	}

	/**
	 * 保存文件(以Context.MODE_PRIVATE私有方式保存)
	 * 
	 * @param fileName文件名称
	 * @param fileContent文件内容
	 * @throws IOException
	 */
	public void save(String fileName, String fileContent) throws IOException {
		// 私有操作模式:创建出来的文件只能被本应用访问
		FileOutputStream outputStream = context.openFileOutput(fileName,
				Context.MODE_PRIVATE);

		outputStream.write(fileContent.getBytes("UTF-8"));

		outputStream.close();
	}

	/**
	 * 保存文件到SD卡
	 * 
	 * @param fileName
	 * @param fileContent
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	public void saveToSDCard(String fileName, String fileContent)
			throws UnsupportedEncodingException, IOException {

		// String dir ="/mnt/sdcard";
		File dir = Environment.getExternalStorageDirectory();
		File file = new File(dir, fileName);
		// new File
		FileOutputStream outputStream = new FileOutputStream(file);
		outputStream.write(fileContent.getBytes("UTF-8"));
		outputStream.close();

	}

	/**
	 * 保存文件(以Context.MODE_APPEND追加保存)
	 * 
	 * @param fileName文件名称
	 * @param fileContent文件内容
	 * @throws IOException
	 */
	public void save_append(String fileName, String fileContent)
			throws IOException {
		//
		FileOutputStream outputStream = context.openFileOutput(fileName,
				Context.MODE_APPEND);

		outputStream.write(fileContent.getBytes("UTF-8"));

		outputStream.close();
	}

	/**
	 * 保存文件(以MODE_WORLD_READABLE保存)
	 * 
	 * @param fileName文件名称
	 * @param fileContent文件内容
	 * @throws IOException
	 */
	public void save_readable(String fileName, String fileContent)
			throws IOException {
		//
		@SuppressWarnings("deprecation")
		FileOutputStream outputStream = context.openFileOutput(fileName,
				Context.MODE_WORLD_READABLE);

		outputStream.write(fileContent.getBytes("UTF-8"));

		outputStream.close();
	}

	/**
	 * 读取文件内容
	 * 
	 * @param fileName文件名称
	 * @return
	 * @throws IOException
	 */
	public String read(String fileName) throws IOException {

		FileInputStream inputStream = context.openFileInput(fileName);
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int size = 0;
		while ((size = inputStream.read(buffer)) != -1) {
			outputStream.write(buffer, 0, size);
		}
		byte[] data = outputStream.toByteArray();

		return new String(data, "UTF-8");

	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值