package com.example.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 android.content.Context;
import android.os.Environment;
public class FileService {
private Context context;
public FileService(Context context){
this.context=context;
}
/**
* 保存文件
* @param name
* @param content
* @throws FileNotFoundException
*/
public void save(String name,String content) throws Exception{
//私有操作模式:创建出来的文件仅能被本应用访问,其它应用无法访问该文件。
//另会覆盖原文件的内容
FileOutputStream o=context.openFileOutput(name, Context.MODE_PRIVATE);
write(content, o);
}
/**
* 写入
* @param content
* @param o
* @throws IOException
*/
private void write(String content, FileOutputStream o) throws IOException {
o.write(content.getBytes());
o.close();
}
/**
* 追加文件
* @param filename
* @param content
* @throws Exception
*/
public void saveAppend(String filename,String content)throws Exception{
FileOutputStream o=context.openFileOutput(filename, Context.MODE_APPEND);//保存在手机自带的文件里面
write(content, o);
}
/**
* 文件,当前文件可以被其他应用读取
* @param filename
* @param content
* @throws Exception
*/
public void saveReadable(String filename,String content)throws Exception{
FileOutputStream o=context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
write(content, o);
}
/**
* 保存文件到SD卡上.需要申请往SD卡写入数据的权限
* @param filename
* @param content
* @throws Exception
*/
public void saveToSDCard(String filename,String content)throws Exception{
File file=new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream o=new FileOutputStream(file);
o.write(content.getBytes());
o.close();
}
/**
* 当前文件可以被其他应用写入
* @param filename
* @param content
* @throws Exception
*/
public void saveWriteable(String filename,String content)throws Exception{
FileOutputStream o=context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE);
write(content, o);
}
/**
* 读取文件
* @param filename
* @return
* @throws Exception
*/
public String read(String filename)throws Exception{
FileInputStream i=context.openFileInput(filename);
ByteArrayOutputStream bs=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
//-1表示读到文件的末尾
while((len=i.read(buffer))!=-1){
bs.write(buffer,0,len);
}
byte[] data=bs.toByteArray();
return new String(data);
}
}
分享到:
相关推荐
Android 读写文件实例,包括 SD 卡读写和 内部存储读写。 文件目录获取等。
Android - 文件读写操作 方法总结
android 通过Environment环境 获取手机根目录 读写 文件操作。
android的IO流读写文件操作示意程序
Android jni文件读写 NDK文件读取 读写 操作 NDK数据加密DEMO 源码直接上传,运行在android6.0系统测试通过
Android SD卡文件读写操作可以学习下
Android读写文件,该文件包含Android里的读写txt文件,方便初学者使用同时欢迎大家交流经验@!
原数据存放在StreamingAsset中,首次启动复制到persistentDataPath,以后进行更新和读取都在persistentDataPath中使用File进行文件操作。需要恢复书序的时候从StreamingAsset中获取即可。
Android文件读写操作总结
实时检测USB的插入,检测并读取文件,也可根据路径进行文件读写操作
1.对读写文件操作的练习 2.对SQLite读写数据库的操作练习。 3.对ListView的练习 4.代码清楚,层次明了。 android sdk 2.3.*
android 读写文件的工具类,对读写操作进行了封装。
android I/O操作,读写操作,本实例源代码测试通过
android开发的文件读写,简单操作。适合初学者参考。
主要介绍了Android 文件读写操作方法总结的相关资料,需要的朋友可以参考下
android文件的创建、删除、 读写、 判断是否操作、文件MD5 java基础学习型
NULL 博文链接:https://z251257144.iteye.com/blog/1355245
Android读写Excel文件的代码例子。用于演示对Excel2003与Excel2007两种格式文件的读取和写入操作。
android 文件操作 输入流对象 输出流对象 SDCard读写