开发家庭记账本小程序必然会用到数据的存储,所以今天主要学习Android开发的数据存储。
##Android开发的数据存储
- 文件存储
- 应用程序只可以把数据存储在自己私有的文件夹里, data/data/<包名>/文件名...
- 上下文:应用程序运行的环境
- api获取目标目录:
-
- this.getFileDir()===>data/data/<包名>/files //保存重要的配置信息
- this.getCacheDir() ===>data/data/<包名>/cache //缓存目录
-
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); } }
-
- sd卡存储
- 应用程序可以把数据存储在外存储卡
-
- 需要声明权限
- Environment.getExternalStorageDirectory() //获取外部存储卡的目录
- 需要注意检查sd卡是否可用,检查sd卡的剩余空间
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(); }
- 文件权限
- 应用程序在data/data/包名/目录下创建的文件都是私有的,其他应用程序是不可以访问的。
- 默认生成的文件都是私有的
- 如何生成公开权限的文件
//生成公开权限的文件 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(); } }
- 更改文件权限的方法
- chmod:change mode Linux下更改文件访问的权限
- 参数 sharedperference
- 声明 sharedperference //sp = this.getSharedPreferences("config", 0);
- 获取到一个参数
- 获取编辑器 //Editor editor = sp.edit();
- editor.putString(key,value); // editor.putInt(); // editor.putDouble();
- editor.commit();保存数据完毕,必须记得调用commit的方法
- 获取数据 sp.getString(key,dafvalue); // sp.getInt(); // sp.getDouble();
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); }