美文网首页
数据存储

数据存储

作者: 姜水伟杰 | 来源:发表于2017-08-02 16:27 被阅读8次

    数据存储

    文件存储

    1应用程序可以把数据存储在自己的私有的文件夹里面,只能存储在自己的文件夹

    / data/data/<包名>/...

    2应用程序可以把数据存储在外存储卡,SD卡(要声明权限)

    Environment。getExternalStorageDirectory()//获取外部存储卡的目录

    3.参数 sharedperference

    //获取原来在文件中保存的QQ号码和密码,回到界面上 
    //File file = new File("/data/data/com.itheima.qqlogin/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.stText(qq);
        et-password.setText(pwd);
      }catch(Exception e){
        e.printStace();
      }
    }
    //存储数据
    try{
    //File file = new File("/data/data/com.itheima.qqlogin/info.txt");
    File file = new File(this.getFilesDir(),"info.txt");
    FileOutputStream fos = new FileOutputStream(file);
    //10065##ahbh
    fos.write((qq+"##"+pwd).getBytes());
    fos.close();
    Toast.makeText(this,"数据保存成功",0).show();  
    }catch(Exception e){
      e.prinStackTrae();
      Toast.makeText(this,"数据保存失败",0).show();
    }
    

    上下文

    应用程序运行的环境

    this.getFilesDir() ----》/data/data/包名/files  保存重要的配置信息
    this.getCacheDir() ----》/data/data/包名/cache  缓存目录
    

    文件权限

    • 应用程序在data/data/自己包名/目录下创建的文件默认都是私有的,别的应用程序是不可以访问的

    相关文章

      网友评论

          本文标题:数据存储

          本文链接:https://www.haomeiwen.com/subject/wihylxtx.html