当前位置: 移动技术网 > IT编程>移动开发>Android > 实例详解Android文件存储数据方式

实例详解Android文件存储数据方式

2019年07月24日  | 移动技术网IT编程  | 我要评论

iriver官网,灵通英语,东风渝安车辆有限公司

总体的来讲,数据存储方式有三种:一个是文件,一个是数据库,另一个则是网络。下面通过本文给大家介绍android文件存储数据方式。

1.文件存储数据使用了java中的io操作来进行文件的保存和读取,只不过android在context类中封装好了输入流和输出流的获取方法。

创建的存储文件保存在/data/data/<package name>/files文件夹下。

2.操作。

保存文件内容:通过context.openfileoutput获取输出流,参数分别为文件名和存储模式。
读取文件内容:通过context.openfileinput获取输入流,参数为文件名。
删除文件:context.deletefile删除指定的文件,参数为将要删除的文件的名称。
获取文件名列表:通过context.filelist获取files目录下的所有文件名数组。

*获取文件路径的方法:

绝对路径:/data/data/<package name>/files/filename

context:context.getfilesdir()可以获取到"/data/data/<package name>/files"

3.四种文件保存的模式。

context.mode_private 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下写入的内容会覆盖原文件的内容。

context.mode_append 检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
mode_world_readable 表示当前文件可以被其他应用读取。
mode_world_writeable 表示当前文件可以被其他应用写入。

在使用模式时,可以用"+"来选择多种模式,比如openfileoutput(filename, context.mode_private + mode_world_readable);

下面通过程序来演示下文件存储的使用。完整代码下载:android_files.rar

/** 
* mainactivity 
* 
* @author zuolongsnail 
* 
*/ 
public class mainactivity extends activity { 
private edittext writeet; 
private button writebtn; 
private textview contentview; 
public static final string filename = "setting.set"; 
@override 
public void oncreate(bundle savedinstancestate) { 
super.oncreate(savedinstancestate); 
setcontentview(r.layout.main); 
writeet = (edittext) findviewbyid(r.id.write_et); 
writebtn = (button) findviewbyid(r.id.write_btn); 
contentview = (textview) findviewbyid(r.id.contentview); 
writebtn.setonclicklistener(new operateonclicklistener()); 
} 
class operateonclicklistener implements onclicklistener { 
@override 
public void onclick(view v) { 
writefiles(writeet.gettext().tostring()); 
contentview.settext(readfiles()); 
system.out.println(getfilesdir()); 
} 
} 
// 保存文件内容 
private void writefiles(string content) { 
try { 
// 打开文件获取输出流,文件不存在则自动创建 
fileoutputstream fos = openfileoutput(filename, 
context.mode_private); 
fos.write(content.getbytes()); 
fos.close(); 
} catch (exception e) { 
e.printstacktrace(); 
} 
} 
// 读取文件内容 
private string readfiles() { 
string content = null; 
try { 
fileinputstream fis = openfileinput(filename); 
bytearrayoutputstream baos = new bytearrayoutputstream(); 
byte[] buffer = new byte[1024]; 
int len = 0; 
while ((len = fis.read(buffer)) != -1) { 
baos.write(buffer, 0, len); 
} 
content = baos.tostring(); 
fis.close(); 
baos.close(); 
} catch (exception e) { 
e.printstacktrace(); 
} 
return content; 
} 
} 

程序截图:

提供一个文件存储数据的工具类:

/** 
* 文件存储数据方式工具类 
* 
* @author zuolongsnail 
*/ 
public class filesutil { 
/** 
* 保存文件内容 
* 
* @param c 
* @param filename 
* 文件名称 
* @param content 
* 内容 
*/ 
private void writefiles(context c, string filename, string content, int mode) 
throws exception { 
// 打开文件获取输出流,文件不存在则自动创建 
fileoutputstream fos = c.openfileoutput(filename, mode); 
fos.write(content.getbytes()); 
fos.close(); 
} 
/** 
* 读取文件内容 
* 
* @param c 
* @param filename 
* 文件名称 
* @return 返回文件内容 
*/ 
private string readfiles(context c, string filename) throws exception { 
bytearrayoutputstream baos = new bytearrayoutputstream(); 
fileinputstream fis = c.openfileinput(filename); 
byte[] buffer = new byte[1024]; 
int len = 0; 
while ((len = fis.read(buffer)) != -1) { 
baos.write(buffer, 0, len); 
} 
string content = baos.tostring(); 
fis.close(); 
baos.close(); 
return content; 
} 
}

以上通过实例详解android文件存储数据方式,希望对大家今后的工作学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网