Android 文件读写 + 申请权限

/**
     *  获取文件
     *  /storage/emulated/0/xxx/xxx.text
     *  /data/xxx/xxx.text
     * @return
     */
    private static File getFile() {
        File file = null;
        //SD卡存在
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            file = Environment.getExternalStorageDirectory();
        } else {
            file = Environment.getDataDirectory();
        }
        file = new File(file.getPath() + BASE_URL);
        if (!file.exists()) {
            file.mkdir();
        }
        file = new File(file.getPath() + FILE_NAME);
        Log.d(TAG, "getFile: " + file.getPath());
        return file;
    }

    /**
     * 写入内容
     * @param content
     */
    public static void write(String content) {
        BufferedWriter writer = null;
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(getFile());
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
                if (out != null)
                    out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 加载文件内容
     * @return
     */
    public static String load() {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            //设置将要打开的存储文件名称
            in = new FileInputStream(getFile());
            //FileInputStream -> InputStreamReader ->BufferedReader
            reader = new BufferedReader(new InputStreamReader(in));
            String line = new String();
            //读取每一行数据,并追加到StringBuilder对象中,直到结束
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        Log.d(TAG, "load: "+ content.toString());
        return content.toString();
    }

Android6.0要验证权限

 /**
     * * 【动态申请SD卡读写的权限】
     * * Android6.0之后系统对权限的管理更加严格了,不但要在AndroidManifest中添加,还要在应用运行的时候动态申请
     * *
     **/
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSON_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE"};

    public static void verifyStoragePermissions(Activity activity) {
        try {
            int permission = ActivityCompat.checkSelfPermission(activity, "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {/**【判断是否已经授予权限】**/
                ActivityCompat.requestPermissions(activity, PERMISSON_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

    原文作者:Sunshine_Cui001
    原文地址: https://blog.csdn.net/Sunshine_Cui001/article/details/105809638
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞