【Android】【转】文件保存与读取
2012-04-28 17:41
447 查看
原文地址:http://tdq222.iteye.com/blog/701161
注:在Activity中有getFileDir()和getCacheDir();方法可以获得当前的手机自带的存储空间中的当前包文件的路径
getFileDir()-----/data/data/cn.xxx.xxx(当前包)/files
getCacheDir()-----/data/data/cn.xxx.xxx(当前包)/cache
1.编写文件读取与写入功能实现类FileService
packagecn.android.service;
importjava.io.ByteArrayOutputStream;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importandroid.content.Context;
importandroid.util.Log;
/**
*文件保存与读取功能实现类
*@authorAdministrator
*
*2010-6-28下午08:15:18
*/
publicclassFileService{
publicstaticfinalStringTAG="FileService";
privateContextcontext;
//得到传入的上下文对象的引用
publicFileService(Contextcontext){
this.context=context;
}
/**
*保存文件
*
*@paramfileName文件名
*@paramcontent文件内容
*@throwsException
*/
publicvoidsave(StringfileName,Stringcontent)throwsException{
//由于页面输入的都是文本信息,所以当文件名不是以.txt后缀名结尾时,自动加上.txt后缀
if(!fileName.endsWith(".txt")){
fileName=fileName+".txt";
}
byte[]buf=fileName.getBytes("iso8859-1");
Log.e(TAG,newString(buf,"utf-8"));
fileName=newString(buf,"utf-8");
Log.e(TAG,fileName);
//Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
//Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
//Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
//MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
//如果希望文件被其他应用读和写,可以传入:
//openFileOutput("output.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
FileOutputStreamfos=context.openFileOutput(fileName,context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
}
/**
*读取文件内容
*
*@paramfileName文件名
*@return文件内容
*@throwsException
*/
publicStringread(StringfileName)throwsException{
//由于页面输入的都是文本信息,所以当文件名不是以.txt后缀名结尾时,自动加上.txt后缀
if(!fileName.endsWith(".txt")){
fileName=fileName+".txt";
}
FileInputStreamfis=context.openFileInput(fileName);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
byte[]buf=newbyte[1024];
intlen=0;
//将读取后的数据放置在内存中---ByteArrayOutputStream
while((len=fis.read(buf))!=-1){
baos.write(buf,0,len);
}
fis.close();
baos.close();
//返回内存中存储的数据
returnbaos.toString();
}
}
2.编写Activity类:
packagecn.android.test;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
importcn.android.service.FileService;
publicclassTestAndroidActivityextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
//得到FileService对象
privateFileServicefileService=newFileService(this);
//定义视图中的filename输入框对象
privateEditTextfileNameText;
//定义视图中的contentText输入框对象
privateEditTextcontentText;
//定义一个土司提示对象
privateToasttoast;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到视图中的两个输入框和两个按钮的对象引用
Buttonbutton=(Button)this.findViewById(R.id.button);
Buttonread=(Button)this.findViewById(R.id.read);
fileNameText=(EditText)this.findViewById(R.id.filename);
contentText=(EditText)this.findViewById(R.id.content);
//为保存按钮添加保存事件
button.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
StringfileName=fileNameText.getText().toString();
Stringcontent=contentText.getText().toString();
//当文件名为空的时候,提示用户文件名为空,并记录日志。
if(isEmpty(fileName)){
toast=Toast.makeText(TestAndroidActivity.this,R.string.empty_filename,Toast.LENGTH_LONG);
toast.setMargin(RESULT_CANCELED,0.345f);
toast.show();
Log.w(fileService.TAG,"Thefilenameisempty");
return;
}
//当文件内容为空的时候,提示用户文件内容为空,并记录日志。
if(isEmpty(content)){
toast=Toast.makeText(TestAndroidActivity.this,R.string.empty_content,Toast.LENGTH_LONG);
toast.setMargin(RESULT_CANCELED,0.345f);
toast.show();
Log.w(fileService.TAG,"Thefilecontentisempty");
return;
}
//当文件名和内容都不为空的时候,调用fileService的save方法
//当成功执行的时候,提示用户保存成功,并记录日志
//当出现异常的时候,提示用户保存失败,并记录日志
try{
fileService.save(fileName,content);
toast=Toast.makeText(TestAndroidActivity.this,R.string.success,Toast.LENGTH_LONG);
toast.setMargin(RESULT_CANCELED,0.345f);
toast.show();
Log.i(fileService.TAG,"Thefilesavesuccessful");
}catch(Exceptione){
toast=Toast.makeText(TestAndroidActivity.this,R.string.fail,Toast.LENGTH_LONG);
toast.setMargin(RESULT_CANCELED,0.345f);
toast.show();
Log.e(fileService.TAG,"Thefilesavefailed");
}
}
});
//为读取按钮添加读取事件
read.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
//得到文件名输入框中的值
StringfileName=fileNameText.getText().toString();
//如果文件名为空,则提示用户输入文件名,并记录日志
if(isEmpty(fileName)){
toast=Toast.makeText(TestAndroidActivity.this,R.string.empty_filename,Toast.LENGTH_LONG);
toast.setMargin(RESULT_CANCELED,0.345f);
toast.show();
Log.w(fileService.TAG,"Thefilenameisempty");
return;
}
//调用fileService的read方法,并将读取出来的内容放入到文本内容输入框里面
//如果成功执行,提示用户读取成功,并记录日志。
//如果出现异常信息(例:文件不存在),提示用户读取失败,并记录日志。
try{
contentText.setText(fileService.read(fileName));
toast=Toast.makeText(TestAndroidActivity.this,R.string.read_success,Toast.LENGTH_LONG);
toast.setMargin(RESULT_CANCELED,0.345f);
toast.show();
Log.i(fileService.TAG,"Thefilereadsuccessful");
}catch(Exceptione){
toast=Toast.makeText(TestAndroidActivity.this,R.string.read_fail,Toast.LENGTH_LONG);
toast.setMargin(RESULT_CANCELED,0.345f);
toast.show();
Log.e(fileService.TAG,"Thefilereadfailed");
}
}
});
}
//编写一个isEmpty方法,判断字符串是否为空
privatebooleanisEmpty(Strings){
if(s==null||"".equals(s.trim())){
returntrue;
}
returnfalse;
}
}
注:在Activity中有getFileDir()和getCacheDir();方法可以获得当前的手机自带的存储空间中的当前包文件的路径
getFileDir()-----/data/data/cn.xxx.xxx(当前包)/files
getCacheDir()-----/data/data/cn.xxx.xxx(当前包)/cache
相关文章推荐
- Android文件保存和读取
- android 打开pdf文件 从inputStream读取数据并保存为文件
- android中文件的保存与读取以及文件的四种操作模式
- Android——文件的保存和读取
- Android文件保存和读取
- android,保存文件到内存和读取,文件访问权限,文件模式
- 保存、方法-Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数-by小雨
- Android——文件的保存和读取
- android 将数据保存到文件中&从文件中读取数据
- android第九步文件的保存和读取
- Android读取url图片保存及文件读取
- Android 文件保存与读取
- android 保存和读取文件
- android 保存和读取文件
- Android开发笔记——从SD卡保存和读取文件
- android 保存和读取文件
- android解压zip到sdcard,并读取解压缩后文件中的xml特定信息,并保存到数据库
- Android--文件保存与读取
- Android保存String到文件和从文件中读取String
- Android文件的读取与保存