Android文件的读取与保存
2015-11-04 20:07
357 查看
原理就是利用java的IO。
openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它。创建的文件保存在/data/data/<packagename>/files目录。
可以通过File Explorer查看。点击右上角的可以导出到电脑里。
openFileOutput()方法的第二参数用于指定操作模式
私有操作模式创建出来的文件只能被本应用所访问,其他应用无法方法该文件,另外采用私有操作模式创建的文件,写入文件中的内容会覆盖原文件的内容
package com.example.service;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context) {
super();
this.context = context;
}
/**
* 保存文件
* @param filename 文件名称
* @param filecontent 文件内容
* @throws Exception
*/
public void save(String filename, String filecontent) throws Exception {
FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(filecontent.getBytes());
outputStream.close();
}
/**
* 读取文件内容
* @param fileName 文件名称
* @return 文件内容
* @throws Exception
*/
public String read(String fileName) throws Exception{
FileInputStream instream = context.openFileInput(fileName);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = instream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
byte []data = outputStream.toByteArray();
return new String(data);
}
}
通过FileInputStream读取了文件之后,要通过FileInputStream的read方法,把信息读到数组中去,然后再通过ByteArrayputStream把数组中的东西读到内存中去。
转载地址http://blog.csdn.net/howlaa/article/details/17285521
openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它。创建的文件保存在/data/data/<packagename>/files目录。
可以通过File Explorer查看。点击右上角的可以导出到电脑里。
openFileOutput()方法的第二参数用于指定操作模式
私有操作模式创建出来的文件只能被本应用所访问,其他应用无法方法该文件,另外采用私有操作模式创建的文件,写入文件中的内容会覆盖原文件的内容
package com.example.service;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.content.Context;
public class FileService {
private Context context;
public FileService(Context context) {
super();
this.context = context;
}
/**
* 保存文件
* @param filename 文件名称
* @param filecontent 文件内容
* @throws Exception
*/
public void save(String filename, String filecontent) throws Exception {
FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(filecontent.getBytes());
outputStream.close();
}
/**
* 读取文件内容
* @param fileName 文件名称
* @return 文件内容
* @throws Exception
*/
public String read(String fileName) throws Exception{
FileInputStream instream = context.openFileInput(fileName);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = instream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
byte []data = outputStream.toByteArray();
return new String(data);
}
}
通过FileInputStream读取了文件之后,要通过FileInputStream的read方法,把信息读到数组中去,然后再通过ByteArrayputStream把数组中的东西读到内存中去。
转载地址http://blog.csdn.net/howlaa/article/details/17285521
相关文章推荐
- 在之前开了一个csdn博客,是关于C语言的,如今开始学习java与android开发,就把从现在开始我的每一天学习都展示在这里吧
- Android样式开发系列
- 个人参考——android之内容提供器provider
- Android设置ImageView对话框背景形状
- Android总结——Android系统框架
- android 非root实现静默安装
- Android AsyncTask原理以及线程池概念
- Android Butterknife框架 注解攻略:
- Android之如何解决Android studio运行出现的HAX kernel modules is not installed
- Android 百分比布局库(percent-support-lib) 解析与扩展
- Glide图片加载变形如何解决?
- Android Tablelayout表格显示问题
- Android生成签名包以及空白包打签名包
- Android开发学习笔记:TextView的属性详解
- Android屏幕适配解决方案
- Android代码混淆导致内部类不可用
- android.view.ViewRootImpl$CalledFromWrongThreadException
- Android 监控软键盘状态
- Android ListView中带有时间数据的排序
- Android中ExpandableListVivew可扩展的ListView(十二)