/**
* 文件存储数据方式工具类
*
* @author liqiwu
*/
public class FileUtil {
/**
* 保存文件内容
*
* @param c
* @param fileName
* 文件名称
* @param content
* 内容
* @throws IOException
* 建立的存储文件保存在/data/data/<package name>/files文件夹下。
*/
public static void writeFiles(Context c, String fileName, String content, int mode) throws IOException {
// 打开文件获取输出流,文件不存在则自动建立
FileOutputStream fos = c.openFileOutput(fileName, mode);
fos.write(content.getBytes());
fos.flush();
fos.close();
} 工具
/**
* 读取文件内容
*
* @param c
* @param fileName
* 文件名称
* @return 返回文件内容
* @throws IOException
*/
public static String readFiles(Context c, String fileName) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = c.openFileInput(fileName);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.flush();
fis.close();
baos.close();
String content = baos.toString();
return content;
}
} .net