Android中读写文件

来源:互联网 发布:淘宝申请定向计划 编辑:程序博客网 时间:2024/06/07 20:54

在Android中读写文件

  • RAM:运行内存,相当于电脑的内存
  • ROM:内部存储空间,相当于电脑的硬盘
    • Android手机必须有的
  • SD卡:外部存储空间,相当于电脑的移动硬盘
    • 不是必须的
  • 现在手机自带的空间都属于外部存储,然后手机基本内部外部共享同一个存储设备

内部存储路径

  • 所有安装至手机的应用都会在data/data目录下生成一个包名文件夹,这个文件夹就是内部存储的路径
  • 应用只能在自己的包名文件夹中读写文件
  • 使用路径api读写文件
  • getFilesDir()得到的file对象的路径是data/data/包名/files
    • 存放在这个路径下的文件,只要你不删,它就一直在
  • getCacheDir()得到的file对象的路径是data/data/包名/cache

    • 存放在这个路径下的文件,是缓存文件,当内存不足时,有可能被删除
  • 系统管理应用界面的清除缓存,会清除cache文件夹下的东西,清除数据,会清除整个包名目录下的东西

       public void login(View v) {        String name = et_name.getText().toString();        String pass = et_pass.getText().toString();        CheckBox cb = (CheckBox) findViewById(R.id.cb);        // 判断选框是否选中        if (cb.isChecked()) {            // 确定文件名和路径            // File file = new File("data/data/包名/info.txt");            // 返回一个File对象,封装的路径是data/data/包名/files            // File file = new File(getFilesDir(), "info.txt");            // 返回一个File对象,封装的路径是data/data/包名/cache            File file = new File(getCacheDir(), "info.txt");            try {                FileOutputStream fos = new FileOutputStream(file);                // 把账号密码写入本地文件                fos.write((name + "&&" + pass).getBytes());                fos.close();            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }
    private void readAccount() {        // 读取文件,回显数据        // File file = new File("data/data/应用程序包名/info.txt");        // File file = new File(getFilesDir(), "info.txt");        File file = new File(getCacheDir(), "info.txt");        if (file.exists()) {            try {                FileInputStream fis = new FileInputStream(file);                // 把字节流转换成字符流                BufferedReader br = new BufferedReader(new InputStreamReader(                        fis));                // 读取文件中的文本                String text = br.readLine();                String s[] = text.split("&&");                // 给输入框设置文本                et_name.setText(s[0]);                et_pass.setText(s[1]);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }

外部存储路径

2.2之前:sdcard
2.2~4.2:mnt/sdcard
4.3开始:storage/sdcard

写sd卡需要权限

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

* 读sd卡,在4.0之前不需要权限,4.0之后可以设置为需要

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • 使用api获得sd卡的真实路径,部分手机品牌会更改sd卡的路径

    Environment.getExternalStorageDirectory()
  • 判断sd卡是否准备就绪

    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
  public void login(View v){        //获取用户输入的账号密码        String name = et_name.getText().toString();        String pass = et_pass.getText().toString();        CheckBox cb = (CheckBox) findViewById(R.id.cb);        //判断选框是否选中        if(cb.isChecked()){//          File file = new File("sdcard/info.txt");            //MEDIA_REMOVED:sd卡不存在            //MEDIA_UNMOUNTED:sd卡存在,但是没有挂载            //MEDIA_CHECKING:sd卡正在遍历            //MEDIA_MOUNTED:sd卡可用            //MEDIA_MOUNTED_READ_ONLY:sd卡可用,但是只读            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){                //返回一个File对象,封装了外部存储的真实路径                File file = new File(Environment.getExternalStorageDirectory(), "info.txt");                try {                    FileOutputStream fos = new FileOutputStream(file);                    //把账号密码写入本地文件                    fos.write((name + "&&" + pass).getBytes());                    fos.close();                } catch (Exception e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            else{                Toast.makeText(this, "SD卡不可用哟亲(づ ̄ 3 ̄づ", 0).show();            }        }    }
  private void readAccount() {//      File file = new File("sdcard/info.txt");        File file = new File(Environment.getExternalStorageDirectory(), "info.txt");        if(file.exists()){            try {                FileInputStream fis = new FileInputStream(file);                //把字节流转换成字符流                BufferedReader br = new BufferedReader(new InputStreamReader(fis));                //读取文件中的文本                String text = br.readLine();                String s[] = text.split("&&");                //给输入框设置文本                et_name.setText(s[0]);                et_pass.setText(s[1]);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }

参考:Android 存储