5.安卓.保存文件到手机 并 读取内容
// 自带存储空间操作***************************************************************
// 自带存储空间操作***************************************************************
/**
* 保存文件,会保存在 data/data/包名/files/这个文件夹里面
* @param context上下文
* @param fileName 文件名
* @param content 内容
* @throws IOException
*/
public static void saveFile(Context context,String fileName,String content) throws IOException
{
//Context.MODE_PRIVATE私有的 会覆盖掉原来的文件
FileOutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
openFileOutput()方法的第二参数用于指定操作模式,有四种模式,分别为:Context.MODE_PRIVATE = 0 私有创建,会覆盖掉原来的内容
Context.MODE_APPEND = 32768 添加,不会覆盖掉原来的内容
Context.MODE_WORLD_READABLE = 1 只能读
Context.MODE_WORLD_WRITEABLE = 2 只能写
out.write(content.getBytes());
}
/**
* 读取文件
* @param context
* @param fileName
* @return
* @throws IOException
*/
public static String readeFile(Context context,String fileName) throws IOException
{
FileInputStream input = context.openFileInput(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte buff[] = new byte[1024];
int len = 0;
while ((len = input.read(buff)) != -1) {
output.write(buff, 0, len);
}
return output.toString();
}

浙公网安备 33010602011771号