經過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