标签:import ati edr open leo imp 目标 bytes lis
开发家庭记账本小程序必然会用到数据的存储,所以今天主要学习Android开发的数据存储。
##Android开发的数据存储
package com.fmd.qqlander; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private static String MainActivity; private static final String TAG = MainActivity; private EditText et_qq; private EditText et_password; private CheckBox cb_remember; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_qq = findViewById(R.id.et_qq); et_password = findViewById(R.id.et_password); cb_remember = findViewById(R.id.cb_remember); //获取原来在文件中保存的QQ号码和密码,回到界面上。 //File file = new File("data/data/com.fmd.qqlander/info.txt"); //getFilesDir() === /data/data/<当前应用程序的包名>/files File file = new File(this.getFilesDir(),"info.txt"); if(file.exists()&&file.length()>0) { try { FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String info = br.readLine(); String qq = info.split("##")[0]; String pwd = info.split("##")[1]; et_qq.setText(qq); et_password.setText(pwd); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void login(View view) { String qq = et_qq.getText().toString().trim(); String pwd = et_password.getText().toString().trim(); if(TextUtils.isEmpty(qq)||TextUtils.isEmpty(pwd)){ Toast.makeText(this, "用户名或密码不能为空", 0).show(); return; } if(cb_remember.isChecked()) { //CheckBox被勾选时记住密码 Log.i(TAG,"记住密码"); }else { //不需要记住密码 Log.i(TAG,"不需要记住密码"); } try { //File file = new File("data/data/com.fmd.qqlander/info.txt"); File file = new File(this.getFilesDir(),"info.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write((qq+"##"+pwd).getBytes()); fos.close(); Toast.makeText(this, "数据保存成功", 0).show(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this, "数据保存异常", 0).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
File file = new File(Environment.getExternalStorageDirectory(),"info.txt"); if(file.exists()&&file.length()>0) { try { FileInputStream fis = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String info = br.readLine(); String qq = info.split("##")[0]; String pwd = info.split("##")[1]; et_qq.setText(qq); et_password.setText(pwd); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
//生成公开权限的文件 public void getpublicFile(View view) { try { FileOutputStream fos = openFileOutput("public.txt",Context.MODE_WORLD_READABLE+Context.MODE_WORLD_READABLE); fos.write("private".getBytes()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public class MainActivity extends Activity { private static String MainActivity; private static final String TAG = MainActivity; private EditText et_qq; private EditText et_password; private CheckBox cb_remember; //1.声明sp private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_qq = findViewById(R.id.et_qq); et_password = findViewById(R.id.et_password); cb_remember = findViewById(R.id.cb_remember); //2.获取到一个参数 sp = this.getSharedPreferences("config", 0); String qq = sp.getString("qq", ""); String pwd = sp.getString("pwd", ""); et_qq.setText(qq); et_password.setText(pwd); } public void login(View view) { String qq = et_qq.getText().toString().trim(); String pwd = et_password.getText().toString().trim(); if(TextUtils.isEmpty(qq)||TextUtils.isEmpty(pwd)){ Toast.makeText(this, "用户名或密码不能为空", 0).show(); return; } if(cb_remember.isChecked()) { //CheckBox被勾选时记住密码 Log.i(TAG,"记住密码"); //3.得到sp文件的编辑器 Editor editor = sp.edit(); editor.putString("qq",qq); editor.putString("pwd",pwd); //4.保存数据完毕,必须记得调用commit的方法 editor.commit(); }else { //不需要记住密码 Log.i(TAG,"不需要记住密码"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }
标签:import ati edr open leo imp 目标 bytes lis
原文地址:https://www.cnblogs.com/fuheishi/p/10419842.html