Android 台灣中文網
標題:
android怎麼實現文件保存
[打印本頁]
作者:
暗桌之光
時間:
2011-6-14 15:28
標題:
android怎麼實現文件保存
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 ut=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();
}
}
複製代碼
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1