设为首页 加入收藏

TOP

Android 自动创建文件到scard卡并保存(二)
2014-11-24 02:01:50 来源: 作者: 【 】 浏览:2
Tags:Android 自动 创建 文件 scard 卡并 保存
ic void saveReadable(String fileNameStr, String fileContentStr)
throws IOException {
// 读取操作模式:可以被其它应用读取,但不能写入
FileOutputStream fos = context.openFileOutput(fileNameStr,
context.MODE_WORLD_READABLE);
fos.write(fileContentStr.getBytes());
fos.close();
}


public void saveWriteable(String fileNameStr, String fileContentStr)
throws IOException {
// 写入操作模式:可以被其它应用写入,但不能读取
FileOutputStream fos = context.openFileOutput(fileNameStr,
context.MODE_WORLD_WRITEABLE);
fos.write(fileContentStr.getBytes());
fos.close();
}


public void saveReadWriteable(String fileNameStr, String fileContentStr)
throws IOException {
// 读写操作模式:可以被其它应用读写
FileOutputStream fos = context.openFileOutput(fileNameStr,
context.MODE_WORLD_READABLE + context.MODE_WORLD_WRITEABLE);
fos.write(fileContentStr.getBytes());
fos.close();
}


/**
* 读取文件内容
*
* @param fileNamestr
* 文件名
* @return
* @throws IOException
*/
public String read(String fileNamestr) throws IOException {
FileInputStream fis = context.openFileInput(fileNamestr);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
byte[] data = bos.toByteArray();


return new String(data);
}


}


注意:在主配置文件中需要添加权限


以上是其主要代码,想下载DEMO的可以到Linux公社资源下载


具体下载目录在 /2013年资料/12月/6日/Android 自动创建文件到scard卡并保存


注意由于这个Demo还有文件上传功能所以还需要你自己修改,选择其中有用的部分


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java事务处理 下一篇Tiny6410内核模块编译之HelloWorld

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: