• Android 文件存储方式


    Android中的数据存储常用方式有五种

    1. 文件存储
    2. SharedPreferences(偏好参数设置)
    3. SQLite数据库
    4. 内容提供者(Content provider)
    5. 网络存储

    1.文件存储
    Context.getCacheDir()方法用于获取/data/data/<package name>/cache目录
    Context.getFilesDir()方法用于获取/data/data/<package name>/files目录

    Activity(Context)提供了openFileOutput(filename, mode)方法用于把数据输出到文件中;
    第一个参数用于指定文件名称,不能包含路径分隔符‘/’
    第二个参数为操作模式:

    • Context.MODE_PRIVATE:私有操作模式创建出来的文件只能被本程序访问,如果文件不存在,会自动创建,另外:写入文件中的内容会覆盖原文件的内容;
    • Context.MODE_APPEND:模式会检查文件是否存在,如果存在则会追加内容,否则创建新文件;
    • Context.MODE_READABLE:表示当前文件可以被其它应用读取;
    • Context.MODE_WRITEABLE:表示当前文件可以被其它应用写入;

    如果希望文件被其它应用读和写:传入(Context.MODE_READABLE+Context.MODE_WRITEABLE)
    FileOutputStream outStream = context.openFileOutput(filename,Context.MODE_PRIVATE); // params(文件名称,操作方式)

    android有一套自己的安全模型,当应用程序(.apk)在安装时系统会分配给一个userid,当该应用去访问其它资源如文件的时候,会进行userid的匹配,默认情况下任何应用创建的文件,sharedpreferences,数据库都是私有的(创建的文件保存在/data/data/<package name>/files目录下),只有指定操作模式为外部可读或写才可以被其它程序访问;

    读取文件1:

    FileInputStream inStream = context.openFileInput(filename);

    读取文件2:

    path="/data/data/<package name>/files/hello.txt";
    File file = new File(path);
    FileInputStream inStream = new FileInputStream(file);

    使用Activity的openFileOutput()方法保存文件,文件是放在手机内存上的,在程序中访问SDCard,需要额外的权限:
    在SDCard中创建与删除文件的权限

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

    在SDCard中写入数据权限

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

    Java代码实现:

    public void saveToSDCard(String filename, String filecontent) throws IOException {
      File file = new File(Environment.getExternalStorageDirectory(),filename); // 获取SDCard的路径
      FileOutputStream outStream = new FileOutputStream(file);
      outStream.write(filecontent.getBytes());
      outStream.close();
    }

    通常在读写SD卡的时候还需要先判断是否可写,下面是判断SD卡是否存在并且可以读写;

    //if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
    //     fs.saveToSDCard(filenamestr, filecontentstr);
    //     Toast.makeText(getApplicationContext(),R.string.success, 1).show();
    //    } else {
    //     Toast.makeText(getApplicationContext(),R.string.sdcarderror, 1).show();
    //    }


    2.SharedPreferences

    用于在开发软件的时候提供软件参数设置,其背后使用的是xml文件存放数据,文件保存在/data/data/<package name>/shared_prefs目录下

    public void savePreferences(String name, Integer value) {
            SharedPreferences preferences = context.getSharedPreferences("itcase", Context.MODE_PRIVATE);
              // 不需要指定文件名的xml后缀名,因为系统知道是xml文件,android会自动添加上;
            Editor editor = preferences.edit();//数据保存在内存之中;
            editor.putString("name", name);
            editor.putInt("age", value);
            editor.commit();// 必须采用此方法把内在中的数据提交回文件中
        }
    
        public Map<String, String> getPreferences() {
            Map<String, String> map = new HashMap<String, String>();
            SharedPreferences preferences = context.getSharedPreferences("itcase", Context.MODE_PRIVATE);// 不需要指定xml后缀名,因为系统知道是xml文件;
            map.put("name", preferences.getString("name", "默认值"));// 如果不存在参数就会返回默认值
            map.put("age", String.valueOf(preferences.getInt("age", 0)));
            return map;
        }

    读取SharedPreferences

      this.getPreferences(mode);//默认会采用activity的名称作为xml的名称;
      PreferenceManager.getDefaultSharedPreferences(Context);
  • 相关阅读:
    Servlet的生命周期
    HTTP协议简单记录
    Tomcat和JavaWeb目录和流程
    02 html 表格表单
    01 初识HTML
    Python 利用pywin32批量将doc转换成docx再读取成一行存入excel
    power bi 数据红绿灯详细用法
    Linux和Windows启动后台程序
    MySQL导出数据字典
    适用于渗透测试不同阶段的工具收集整理
  • 原文地址:https://www.cnblogs.com/a284628487/p/3001031.html
Copyright © 2020-2023  润新知