【安卓学习笔记】文件存储读写

安卓的文件存储读写主要有两种:

1、data/data/包名/files 目录下存储。

这种存储是将文件存到手机内存中对应的包名下。软件卸载后,存在这里的文件也全部被系统删除。此外,存在这里的文件可定义访问权限:

图片截取自菜鸟教程
2、SD卡下存储。

常用于一些媒体文件,如图片、音视频、大文件等。

使用示例:

1、读写包名下文件

public class FileHelp {

        private Context context;

        public FileHelp(Context context) {
            this.context = context;
        }

        public void fileWrite(String fileName, String fileContent) {

            try {
                FileOutputStream fos = context.openFileOutput(fileName,
                        MODE_PRIVATE);
                fos.write(fileContent.getBytes());
                fos.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public String fileRead(String fileName) {

            try {
                FileInputStream fis = context.openFileInput(fileName);
                StringBuffer sb = new StringBuffer();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) != -1) {
                    sb.append(new String(buffer, 0, len));
                }
                fis.close();
                return sb.toString();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

    }

2、SD卡读写

public class SDFileHelper {

        public void fileWrite(String fileName, String content) {

            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                try {
                    fileName = Environment.getExternalStorageDirectory()
                            .getCanonicalPath() + "/" + fileName;

                    FileOutputStream fos = new FileOutputStream(fileName);

                    fos.write(content.getBytes());
                    fos.close();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public String fileRead(String fileName) {

            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                fileName = Environment.getExternalStorageDirectory() + "/"
                        + fileName;

                try {
                    FileInputStream fis = new FileInputStream(fileName);
                    StringBuffer sb = new StringBuffer();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = fis.read(buffer)) != -1) {
                        sb.append(new String(buffer, 0, len));
                    }
                    fis.close();
                    return sb.toString();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            return null;
        }

    }

这两种方式基本差不多,区别在于方式1是openFileInput/openFileOutput,方式2是直接new一个文件流。另外,方式2需要先判断SD卡是否挂载,然后获取SD卡根目录。

参考自 http://www.runoob.com/w3cnote/android-tutorial-file.html

你可能感兴趣的:(【安卓学习笔记】文件存储读写)