通过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】来指定了
总结以上是内存溢出为你收集整理的Android中关于IO储存的小记全部内容,希望文章能够帮你解决Android中关于IO储存的小记所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)