数据的保存和读取
1数据的保存
btn_ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String user = et_user.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); if(user.isEmpty()||pwd.isEmpty()){ Toast.makeText(getApplicationContext(), "不能为空", 200).show(); }else{ String name="data.txt"; String content=user+"="+pwd; try { FileOutputStream fos=openFileOutput(name, MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); Toast.makeText(getApplicationContext(), "保存成功", 200).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); |
2,数据的读取
btn_show.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String name="data.txt"; try { FileInputStream fis=openFileInput(name); BufferedReader br=new BufferedReader(new InputStreamReader(fis)); //读取文档的数据 String content = br.readLine(); //拆分字符串 String[] strs = content.split("="); //显示的数据 if(!content.isEmpty()){ et_user.setText(strs[0]); et_pwd.setText(strs[1]); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); |