Android数据存储——文件ANDsharepreferences

来源:互联网 发布:qq飞车风火轮数据 编辑:程序博客网 时间:2024/06/15 04:35
public class MainActivity extends ActionBarActivity {


    private EditText number;
private EditText pwd;
private Button ensure;
private CheckBox checkb;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    //获取界面中的控件
    number = (EditText) findViewById(R.id.et_number);
    pwd = (EditText) findViewById(R.id.et_pwd);
    ensure = (Button) findViewById(R.id.but_ensure);
    checkb = (CheckBox) findViewById(R.id.checkB);
    
    //回显账号和密码
    Map<String, String> map=Utils.getdata();
    if (map!=null) {
number.setText(map.get("number"));
pwd.setText(map.get("pwd"));
}
    //为按钮设置监听
    ensure.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// 判断时候是否选中记住密码
if (checkb.isChecked()) {
//获取文本框和密码框的内容
String tx_number = number.getText().toString().trim();
String tx_pwd = pwd.getText().toString().trim();
//新建一个类,调用里面的方法
Utils.save(tx_number,tx_pwd);
//来个吐丝
Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show();
}
}
});
    //*******************轻量级数据存储——使用sharedpreferences 生成XML格式文件*******************
    /**
           实现SharedPreferences存储的步骤如下:
一、根据Context获取SharedPreferences对象
二、利用edit()方法获取Editor对象。
三、通过Editor对象存储key-value键值对数据。
四、通过commit()方法提交数据。
    **/
    //获取文本框和密码框的内容
  String tx_number = number.getText().toString().trim();
  String tx_pwd = pwd.getText().toString().trim();
    //获取sharedpreferences对象,SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
    Context ctx=MainActivity.this;
    SharedPreferences sp=ctx.getSharedPreferences("SP", MODE_PRIVATE);
    Editor et=sp.edit();
    et.putString("number", tx_number);
    et.putString("pwd", tx_pwd);
    et.commit();
    }

}


public class Utils {
static //定义文件保存的路径
String path="/data/data/com.example.logindemo/userinfo.txt";
public static void save(String tx_number, String tx_pwd) {
// 使用IO流保存为txt文件,具体输入/还是输出,相对内存————硬盘来说,从内存出去——输出流,放入内存——输入流
try {
FileOutputStream fos=new FileOutputStream(path);
String data=tx_number+"#"+tx_pwd;
fos.write(data.getBytes());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
public static Map<String, String> getdata() {
//定义一个MAP存放数据
Map<String, String> map=new HashMap<String, String>();
try {
// 使用输入流读取数据
FileInputStream fis = new FileInputStream(path);
//使用buffer
BufferedReader buf=new BufferedReader(new InputStreamReader(fis));
//调用bufferedreader里面的方法
String str = buf.readLine();
//分割字符串
String [] array_data=str.split("#");
map.put("number", array_data[0]);
map.put("pwd", array_data[1]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return map;
}


}