Android -- Android数据存储

⒈SharedPreferences轻量级数据存储【通常用来存储例如App的一些设置信息、用户名密码等】

  • Xml文件,K-V形式
  • SharedPreferences,通过它可以完成对数据读的操作。
  • SharedPreferences.Editor,通过它可以完成对数据写的操作。

  1.文件目录

  /data/data/<applicationId>/shared_prefs

  **包名和applicationId不一样

  applicationId可以修改

  Android Studio =》 View=》 Tool Windows =》 Device File Explorer 

  2.使用方式

        //数据存储
        SharedPreferences sharedPreferences = null;
        SharedPreferences.Editor editor = null;

        //第一个参数为文件名
        //第二个参数为模式
                //MODE_PRIVATE --- 这个文件只有当前应用可以进行读写,其他App无法读写,也获取不到【常用】
                //MODE_WORLD_READABLE --- 其他的应用可以读取
                //MODE_WORLD_WRITEABLE --- 其他的应用可以写
                //MODE_APPEND --- 在文件追加写入,不会覆盖文件
        sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.putString("name","fanqi");
        //commit是一个同步存储的过程,会阻塞线程,apply是一个异步的过程,会在后台进行,推荐apply
        editor.apply();    //需要提交后才能生效
        //editor.commit();

        //读取数据
        //参数值为key和缺省值,如果没有取到则返回缺省值
        sharedPreferences.getString("name","");

⒉Android存储概念

  分类

    • 内部存储【Internal Storage】,随应用卸载而被删除
      • /data/data/<applicationId>/shared_prefs
      • /data/data/<applicationId>/databases
      • /data/data/<applicationId>/files【context.getFilesDir()】
      • /data/data/<applicationId>/cache【context.geteCacheDir()】
    • 外部存储【External Storage】 
      • 共有目录【Environment.getExternalStoragePublicDirectory(int type)】
      • 私有目录【Android】,随应用卸载而被删除
        • /mnt/sdcard/Android/data/data/<applicationId>/cache【】
        • /mnt/sdcard/Android/data/data/<applicationId>/files【】

⒊File内部存储

  • 利用Java的I/O流
  • FileOutputStream FileInputStream
    private void save(String content){
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE);
            fileOutputStream.write(content.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private String read(){
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = openFileInput("test.txt");
            byte[] buff = new byte[1024];
            StringBuilder builder = new StringBuilder();
            int len = 0;
            while ((len = fileInputStream.read(buff)) > 0){
                builder.append(new String(buff,0,len));
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

⒋File外部存储

    private void save(String content){
        FileOutputStream fileOutputStream = null;
        try {
            //fileOutputStream = openFileOutput("test.txt",MODE_PRIVATE);
            File dir = new File(getExternalFilesDir(null),"coreqi");
            if(!dir.exists()){
                dir.mkdir();
            }
            File file = new File(dir,"test.txt");
            if(!file.exists()){
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(content.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private String read(){
        FileInputStream fileInputStream = null;
        try {
            //fileInputStream = openFileInput("test.txt");
            File file = new File(getExternalFilesDir(null).getAbsolutePath() + File.separator + "coreqi","test.txt");
            fileInputStream = new FileInputStream(file);
            byte[] buff = new byte[1024];
            StringBuilder builder = new StringBuilder();
            int len = 0;
            while ((len = fileInputStream.read(buff)) > 0){
                builder.append(new String(buff,0,len));
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

  在AndroidManifest.xml中声明SD卡权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  **Android6.0之后的权限需要动态的获取,即如果你的build.gradle中compileSdkVersion的版本是23或23以上的,那么你需要考虑6.0以上动态权限的问题。

  **动态申请权限

        //字符串为要申请权限的数组
        ActivityCompat.requestPermissions(this,new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        },1);

    

原文地址:https://www.cnblogs.com/fanqisoft/p/12171324.html