android 操作文件
发布日期:2021-05-24 09:07:35
浏览次数:6
分类:技术文章
本文共 2788 字,大约阅读时间需要 9 分钟。
package com.liyong; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import android.content.Context; public class FileService { private Context context; public FileService(Context context) { super(); this.context = context; } /** * 写入文件到SD卡 * @throws IOException */ public void saveToSD(String fileNameStr, String fileContentStr) throws IOException{ //备注:Java File类能够创建文件或者文件夹,但是不能两个一起创建 File file = new File("/mnt/sdcard/myfiles"); // if(!file.exists()) // { // file.mkdirs(); // } // File sd=Environment.getExternalStorageDirectory(); // String path=sd.getPath()+"/myfiles"; // File file=new File(path); // if(file.exists()){ // file.delete(); // } if(!file.exists()){ file.mkdir(); } File file1 = new File(file, fileNameStr); FileOutputStream fos = new FileOutputStream(file1); fos.write(fileContentStr.getBytes()); fos.close(); } /** * 保存文件到手机 * @param fileNameStr 文件名 * @param fileContentStr 文件内容 * @throws IOException */ public void save(String fileNameStr, String fileContentStr) throws IOException { //私有操作模式:创建出来的文件只能被本应用访问,其它应用无法访问该文件,另外采用私有操作模式创建的文件,写入文件中的内容会覆盖原文件的内容 FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_PRIVATE); fos.write(fileContentStr.getBytes()); fos.close(); } public void saveAppend(String fileNameStr, String fileContentStr) throws IOException { //追加操作模式:不覆盖源文件,但是同样其它应用无法访问该文件 FileOutputStream fos = context.openFileOutput(fileNameStr, context.MODE_APPEND); fos.write(fileContentStr.getBytes()); fos.close(); } public 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); } }转载地址:https://blog.csdn.net/liyong1028826685/article/details/88289139 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月24日 20时06分13秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
YAPF —— Python代码格式化工具
2019-04-30
UGC 用户产生内容
2019-04-30
ranger
2019-04-30
slurm
2019-04-30
xfce4
2019-04-30
xrdp
2019-04-30
Raft算法
2019-04-30
Python计算文本BLEU分数
2019-04-30
swap内存(linux)
2019-04-30
torch.distributed 分布式
2019-04-30
PyPy
2019-04-30
MATLAB与CUDA
2019-04-30
Linux png转jpg (convert命令)
2019-04-30
NAS (Network Attached Storage 网络附属存储)
2019-04-30
Ubuntu更新后终端中字体的颜色全是白色
2019-04-30
vscode git
2019-04-30
基于MATLAB的二进制数字调制与解调信号的仿真——2FSK
2019-04-30
基于MATLAB的二进制数字调制与解调信号的仿真——2PSK
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——AM
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——DSB
2019-04-30