android—檔案儲存之Propertes

來源:互聯網
上載者:User

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();
}
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.