a b c d e f g h i j k l m n o p q r s t u v w x y z

android---文件保存之Propertes

Properties(属性),可以把Properties继承自Hashtable,理解成一个Hashtable ,不过唯一不同的是,Properties对应的“键-值”必须是字符串形式的数据类型。Files 数据存储主要是使用 Properties 配合 FileInputStream或者FileOutputStream对文件写入操作。

 

代码
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==btnlogin.getId()){
Properties properties
=new Properties();
String name
=etname.getText().toString();
String pwd
=etpwd.getText().toString();
try {
// 文件创建模式:MODE_APPEND
// 如果该文件已经存在,然后将数据写入,而不是抹掉它现有文件的末尾。
// 文件创建模式:MODE_PRIVATE
// 默认模式,在那里创建的文件只能由应用程序调用,即为私有的
// 文件创建模式:MODE_WORLD_READABLE
// 允许所有其他应用程序有读取和创建文件的权限。
// 文件创建模式:MODE_WORLD_WRITEABLE
// 允许所有其他应用程序具有写入、访问和创建的文件权限。
FileOutputStream out=this.openFileOutput("login.cfg",Context.MODE_PRIVATE);
properties.put(
"name", name);
properties.put(
"pwd", pwd);
try {
properties.store(
out, "");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
loadInfo();
}
}

//保存文件
public void loadInfo(){
Properties properties
=new Properties();
try {
FileInputStream
in=this.openFileInput("login.cfg");
try {
properties.load(
in);
String name
=properties.get("name").toString()+"///";
String pwd
=properties.get("pwd").toString()+"///";
this.etname.setText(name);
this.etpwd.setText(pwd);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

 

posted @ 2010-11-01 16:57  莴笋炒肉  阅读(816)  评论(0编辑  收藏  举报