Android中数据存储——文件存储数据

  当一个应用程序在Android中安装后,我们在使用应用的过程中会产生很多的数据,应用都有自己的数据,那么我们应该如何存储数据呢?

数据存储方式

Android 的数据存储有5种方式:

1. SharedPreferences存储数据
   SharedPreferences数据存储,也叫作xml存储。这是将数据存储“data/data/程序包名/share_prefs”路径下的到xml文件中。
相关连接:《Android中数据存储——SharedPreferences存储数据 》  
2. 文件存储数据
   分为内部储存和外部存储。内部存储是应用程序使用Android为自己分配的内存空间,数据存储到“/data/data/程序包名/files”路径下的相应文件中。外部存储是使用手机sdcard的内存(这个sdcard并不是我们经常说的那个可以拆卸替换的SD卡,那个SD卡我们称之为扩展卡),使用这部分内存要声明相应的权限。  
相关连接:《Android中数据存储——文件存储数据》 
3. SQLite数据库存储数据
  使用数据库进行存储,这个一般数据量比较大的时候。
相关连接:《Android中数据存储——SQLite数据库存储数据 》 
4. 使用ContentProvider存储数据
  这个比较眼熟,ContentProvider也是Android的四大组件之一。ContentProvider一般是第三方提供的数据存储方式,向我们手机中的通讯录联系人,照片,音乐等……  
相关连接:《Android中数据存储——ContentProvider存储数据 》
5. 网络存储数据
   这个是将数据上传到网络上进行存储。

   
  下面进入我们今天的主要内容,使用文件存储数据。

文件存储数据

  文件存储是Android中最基本的一种存储方式,他不对存储的内容进行任何格式化的处理,所有的数据都是原封不动的保存在文件中的,因此他比较适合于存储一些简单的二进制数据或文本数据。
  文件存储也分两种:内部存储和外部存储。
  

内部存储

  指Android为应用程序分配的内存。
  通过Context类中封装好的输入流和输出流的获取方法获得数据/data/data//files目录下存储的数据。

files目录下写数据

  通过Context的封装好的方法 openFileOutput(String filename, int mode)获得数据流:
String filename参数:在/data/data//files目录下存储时的文件名。
int mode参数:文件的操作模式,主要有两种模式可选择:MODE_PRIVATEMODE_APPEND。默认为MODE_PRIVATE,当指定相同文件名进行读写的时候,新的内容会覆盖原有内容;MODE_APPEND模式会在已存在的文件的最后追加新的内容。除此之外有两种模式:MODE_WORLD_READABLEMODE_WORLD_WRITEABLE,这两种模式表示允许其他应用程序对我们的程序文件进行读写操作。

        try {
            FileOutputStream fileOutputStream = openFileOutput("cache_text", MODE_PRIVATE);
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(fileOutputStream));
            writer.write("你好缓存!");
            writer.flush();
            writer.close();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
files目录下读数据

   通过Context的封装好的方法 openFileInput(String filename)获得数据流:
String filename参数:在/data/data//files目录下读取时的文件名。

    try {
            FileInputStream fileInputStream = openFileInput("cache_text");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String line = bufferedReader.readLine();
            while (line != null) {
                Log.d("data", "" + line);
                line = bufferedReader.readLine();
            }
            bufferedReader.close();
            fileInputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

  通过Context类中getCacheDir()方法获得”/data/data//cache“目录,创建相关的文件存储数据。

cache目录下写数据
        File file = new File(getCacheDir(), "cache_dir_text");//创建/data/data/<package name>/cache/cache_dir_text文件对象。
        if (!file.exists()) {
            try {
                file.createNewFile();//如果文件不存在,则创建
                FileOutputStream fos = new FileOutputStream(file);
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos));
                writer.write("你好缓存!啊啊啊啊");
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
cache目录下读数据  
        File file = new File(getCacheDir(), "cache_dir_text");
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String line = br.readLine();
            while(line!=null){
                Log.d("data", " " + line);
                line = br.readLine();
            }
            br.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

外部存储

  手机的内存空间,手机内置sdCard(非扩展卡)。使用外部存储要在AndroidManifext.xml中加入访问权限:

    <!-- 允许程序写入外部存储,如SD卡上写文件-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
写数据
        //创建sccard_text文件对象,由于不同手机SDcard目录不同,所以我们通过Environment.getExternalStorageDirectory()获得路径。
        File file = new File(Environment.getExternalStorageDirectory(), "sccard_text");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileOutputStream fos =new FileOutputStream(file);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
            bw.write("你好啊");
            bw.flush();
            bw.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
读数据
         File file = new File(Environment.getExternalStorageDirectory(), "sccard_text");
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br =  new BufferedReader(new InputStreamReader(fis));
            String line = br.readLine();
            while(line!=null){
                Log.d("data", " " + line);
                line = br.readLine();
            }
            br.close();
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小_爽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值