android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
来源:互联网 发布:新西兰生活知乎 编辑:程序博客网 时间:2024/05/17 23:04
在介绍如何在Android平台下进行文件的读取之前,有必要了解Android平台下的数据存储规则。在其他的操作系统如Windows 平台下,应用程序可以自由地或者在特定的访问权限基础上访问或修改其他应用程序名下的文件等资源,而在Android平台下,一个应用程序中所有的数据都是私有的。
当应用程序被安装到系统中后,其所在的包会有一个文件夹用于存放自己的数据,只有这个应用程序才有对这个文件夹的写入权限,这个私有的文件夹位于Android系统的/data/data/<应用程序包名>目录下,其他的应用程序都无法再这个文件夹中写入数据。除了存放私有的数据文件夹外,应用程序也具有SD卡的写入权限。
使用文件I/O 方法可以直接往手机中存储数据,默认情况下这些文件不可以被其他的应用程序访问。Android平台支持 java平台下的 文件I/O操作, 主要使用FileInputStream 和 FileOutputStream 这两个类来实现文件的存储与读取。获取这两个类对象的方式有两种。
一:第一种方式就是像Java平台下的实现方式一样通过构造器直接创建,如果需要向打开的文件末尾写入数据,可以通过使用构造器FileOutputStream(File file, boolean append)将 append设置为true来实现。不过需要注意的是采用这种方式获得FileOutputStream 对象时如果文件不存在或不可写入时,会抛出 FileNotFoundException 异常。
二:第二种获取 FileInputStream 和 FileOutputStream 对象的方式是调用 Context.openFileInput 和 Context.openFileOutput两个方法来创建。除了这两个方法外,Context对象还提供了其他几个用于对文件操作的方法,如下所示
Context对象中文操作的API及说明
说明openFileInput(String filename)
打开应用程序私有目录下的的指定私有文件以读入数据,返回一个FileInputStream 对象
openFileOutput
打开应用程序私有目录下的的指定私有文件以写入数据,返回一个FileOutputStream 对象,
如果文件不存在就创建这个文件。
fileList()搜索应用程序私有文件夹下的私有文件,返回所有文件名的String数组deleteFile(String fileName)删除指定文件名的文件,成功返回true,失败返回false
在使用openFileOutput方法打开文件以写入数据时,需要指定打开模式。默认为零,即MODE_PRIVATE。不同的模式对应的的含义如下:
openFileOutput方法打开文件时的模式
含义MODE_PRIVATE
默认模式,文件只可以被调用该方法的应用程序访问MODE_APPEND
如果文件已存在就向该文件的末尾继续写入数据,而不是覆盖原来的数据。MODE_WORLD_READABLE
赋予所有的应用程序对该文件读的权限。MODE_WORLD_WRITEABLE
赋予所有的应用程序对该文件写的权限。
下面通过一个小例子来说明Android平台下的文件I/O 操作方式,主要功能是在应用程序私有的数据文件夹下创建一个文件并读取其中的数据显示到屏幕的 TextView中,这个例子也比较简单只有一个类。
先看一下运行后的效果吧。
package jcodecraeer.com;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class Activity01 extends Activity{
//常量,为编码格式
public static final String ENCODING =
"UTF-8"
;
//定义文件的名称
String fileName =
"test.txt"
;
//写入和读出的数据信息
String message =
"欢迎大家来www.jcodecraeer.com"
;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
writeFileData(fileName, message);
String result = readFileData(fileName);
textView = (TextView)findViewById(R.id.tv);
textView.setTextColor(Color.GREEN);
textView.setTextSize(20.0f);
textView.setText(result);
}
//向指定的文件中写入指定的数据
public void writeFileData(String filename, String message){
try
{
FileOutputStream fout = openFileOutput(filename, MODE_PRIVATE);
//获得FileOutputStream
//将要写入的字符串转换为byte数组
byte[] bytes = message.getBytes();
fout.write(bytes);
//将byte数组写入文件
fout.close();
//关闭文件输出流
}
catch
(Exception e) {
e.printStackTrace();
}
}
//打开指定文件,读取其数据,返回字符串对象
public String readFileData(String fileName){
String result=
""
;
try
{
FileInputStream fin = openFileInput(fileName);
//获取文件长度
int lenght = fin.available();
byte[] buffer =
new
byte[lenght];
fin.read(buffer);
//将byte数组转换成指定格式的字符串
result = EncodingUtils.getString(buffer, ENCODING);
}
catch
(Exception e) {
e.printStackTrace();
}
return
result;
}
}
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- Android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- android 文件存储(openFileOutPut 和 openFileInput())
- 使用openFileOutput和openFileInput读取文件
- Android数据存储之文件 openFileOutput & openFileInput
- 使用openFileInput和openFileOutput实现Android平台的数据存储
- Android openFileOutput 和 openFileInput
- Android openFileOutput 和 openFileInput
- Android文件操作中的openFileOutPut和openFileInput
- Android下openfileoutput和openfileinput的使用
- 文件存储openFileOutput和openFileInput 和String.getBytes()
- Caused by: java.net.ConnectException: Connection timed out: connect||Caused by: com.mysql.jdbc.excep
- 线性表操作函数实现(P19)---------顺序表示
- 焦点图效果,点击小图显示大图
- 用IEC定时器如何使用多重背景
- 何为js的语句声明
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- [NOIP2008]笨小猴 T1
- android ftp util
- FreeBSD下修改安装源的方法
- PowerDesigner之需求模型
- EXPORT_SYMBOL解析
- Apache Mesos【二】Apache Mesos底层基础库
- C#接口作用的深入理解
- 凸包 graham旋转扫描