一、基本概念
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上,则不受读写控制。
二、组合使用
- FileOutputStream outStream = this.openFileOutput("xy.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
允许其他应用读写,并默认覆盖
- 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");
}
}