欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发基础教程—文件存储功能实现

程序员文章站 2024-02-11 10:14:22
文件存储: 复制代码 代码如下: public class mainactivity extends activity { edittext mname, mage; te...
文件存储:
复制代码 代码如下:

public class mainactivity extends activity {
edittext mname, mage;
textview mtv;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
mname = (edittext) findviewbyid(r.id.edittext1);
mage = (edittext) findviewbyid(r.id.edittext2);
mtv = (textview) findviewbyid(r.id.textview1);
}
public void onclick(view v) {
string name = mname.gettext().tostring();
int age = integer.parseint(mage.gettext().tostring());
string cont = "name=" + name + ",age=" + age + "\n";
try {
int id = v.getid();
// 内部保存
if (id == r.id.button1) {
fileoutputstream fos = this.openfileoutput("mytext.txt",
context.mode_append | context.mode_world_writeable
| context.mode_world_readable);
fos.write(cont.getbytes());
fos.close();
toast.maketext(this, "写入完成", 1).show();
}
// 读取
else if (id == r.id.button2) {
fileinputstream fis = this.openfileinput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
string str = new string(bytes);
mtv.settext(str);
}
} catch (exception e) {
e.printstacktrace();
}
}

其他app如果想要访问这个mytext.txt文件格式如下:
复制代码 代码如下:

public class mainactivity extends activity {
textview mcontent;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
mcontent=(textview) findviewbyid(r.id.textview1);
}
public void onclick(view v){
switch (v.getid()) {
case r.id.button1:
try {
readremotefilebyabslutepath();
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
break;
case r.id.button2:
try {
writeremotefilebyabslutepath();
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
break;
default:
break;
}
}
/**
* 通过文件绝对路径读取远程文件
* @throws exception
*/
public void readremotefilebyabslutepath() throws exception{
string path = "/data/data/com.nanguabing.filedemo/files/mytext.txt" ;
fileinputstream fis = new fileinputstream(path);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
string str = new string(bytes);
mcontent.settext(str);
log.i("other", str);
}
/**
* 通过文件绝对路径读取远程文件
* @throws exception
*/
public void writeremotefilebyabslutepath() throws exception{
string path = "/data/data/com.nanguabing.filedemo/files/mytext.txt" ;
fileoutputstream fos = new fileoutputstream(path,true);
fos.write("other write! ".getbytes());
fos.close();
log.i("other", "other write over!");
}
/**
* 通过包相关上下文写入远程文件
* @throws exception
*/
public void readromotebypackagecontext() throws exception {
string pname = "com.nanguabing.filedemo";
context ctx = this.createpackagecontext(pname,
context.context_ignore_security);
fileinputstream fis = ctx.openfileinput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
log.i("other",new string(bytes));
}
/**
* 通过包相关上下文写入远程文件
*/
public void readromotebypackagecontext2() throws exception {
string pname = "com.nanguabing.filedemo";
context ctx = this.createpackagecontext(pname,
context.context_include_code);
fileinputstream fis = ctx.openfileinput("mytext.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
log.i("other",new string(bytes));
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.activity_main, menu);
return true;
}
}