通过Shared Preferences存储数据
private SharedPreferences mrsoft;//创建对象
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qq2);
final EditText qqzh = findViewById(R.id.qqzh);
final EditText qqmm = findViewById(R.id.qqmm);
Button qqdl=findViewById(R.id.qqdl);
mrsoft = getSharedPreferences("mrsoft", MODE_PRIVATE);
String username= mrsoft.getString("username",null);//获取账号,默认值设为mr
String password= mrsoft.getString("password",null);
if(username!=null && password!=null)
{
qqzh.setText(username);
qqmm.setText(password);
// if (username.equals(mr)&&password.equals(mrsofts)) {
// Intent intent = new Intent(qq2.this, duo.class);
// startActivity(intent);
//}
}
qqdl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String in_username = qqzh.getText().toString();
String in_password = qqmm.getText().toString();
SharedPreferences.Editor editor = mrsoft.edit();
//if (in_username.equals(mr) && in_password.equals(mrsofts)) {
editor.putString("username1", in_username);
editor.putString("password1", in_password);
Log.i("调试是否获取到", "执行到此");
Intent intent = new Intent(qq2.this, duo.class);
startActivity(intent);
Toast.makeText(qq2.this, "已保存密码", Toast.LENGTH_SHORT).show();
editor.commit();
//}
//else {
// Toast.makeText(qq2.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
//}
}
});
}
总结 对于Shared Preferences来说他出存储的位置在data/data目录下手机无法直接查看,而且以xml文件的方式存储到本地;xml文件如下所示
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="specialtext">hajsdh><?//</string>
<string name="username">dsa</string>
<string name="password">dasdasd</string>
<int name="int" value="47" />
<boolean name="or" value="true" />
</map>
储存的步骤:
1.通过mrsoft = getSharedPreferences("mrsoft", MODE_PRIVATE);
方法先获取到一个sharp对象
2.通过SharedPreferences.Editor editor = mrsoft.edit();
获取到sharp的一个内部对象editor才能进行存储操作
3.直接put即可,有点类似map集合前key后 valueeditor.putString("username1", in_username); editor.putString("password1", in_password);
读取的操作:
1.通过mrsoft = getSharedPreferences("mrsoft", MODE_PRIVATE);
方法先获取到一个sharp对象
2.这样即可String username= mrsoft.getString("username",null);//获取账号,默认值设为mr String password= mrsoft.getString("password",null);
通过IO流对象存储读取对象
public class jishi extends AppCompatActivity {
byte[] buffer=null;
File file;
private EditText jis;
private FileInputStream fis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jishi);
Button baochun = findViewById(R.id.baochun);
jis = findViewById(R.id.ji);
file=new File(Environment.getExternalStorageDirectory(),"new.txt");
try {
fis = new FileInputStream(file);
buffer =new byte[fis.available()];
fis.read(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
String data=new String(buffer);
jis.setText(data);
} catch (IOException e) {
e.printStackTrace();
}
}
baochun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileOutputStream fos=null;
String text= jis.getText().toString();
try {
fos=new FileOutputStream(file);
fos.write(text.getBytes());
fos.flush();
Toast.makeText(jishi.this,"保存成功",Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(jishi.this,"文件不存在异常",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(jishi.this,"io异常",Toast.LENGTH_SHORT).show();
}finally {
if(fos!=null)
{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
}
创建io对象:然后获取读取没什么区别,就是读取流中I有个
fis = new FileInputStream(file);
buffer =new byte[fis.available()];
fis.read(buffer);
看这个片段,不再通过指定byte【1024】来指定了