经过Shared Preferences存储数据java
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文件以下所示ide
<?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);
this
读取的操做:
1.经过mrsoft = getSharedPreferences("mrsoft", MODE_PRIVATE);
方法先获取到一个sharp对象
2.这样便可String username= mrsoft.getString("username",null);//获取帐号,默认值设为mr String password= mrsoft.getString("password",null);
spa
经过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有个code
fis = new FileInputStream(file); buffer =new byte[fis.available()]; fis.read(buffer);
看这个片断,再也不经过指定byte【1024】来指定了xml