安卓文件存储

来源:互联网 发布:阿里云香港主机 编辑:程序博客网 时间:2024/05/27 14:13

安卓文件存储

保存路径 应用程序的包名下的file
这里写图片描述

// 保存数据    public void saveData(String saveStr,String saveFileName) {// 判断SD卡是否可用        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){            // TODO        }        FileOutputStream fos = null;        BufferedWriter bw = null;        try {            /**             * 打开文件输出流 需要上下文             * @param name 存储文件的名称             * @param int 保存类型             */            fos = openFileOutput(saveFileName, Context.MODE_PRIVATE);            // 创建缓冲写入器 BufferedWriter会自动刷新缓冲区            bw = new BufferedWriter(new OutputStreamWriter(fos));            bw.write(saveStr);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (bw != null) {                try {                    // 关闭缓冲写入器                    bw.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    // 读取数据    public String readData(String saveFileName) {        FileInputStream fis = null;        BufferedReader br = null;        StringBuilder sb = new StringBuilder();        try {            // 打开文件输入流            fis = openFileInput(saveFileName);            // 创建缓冲读取器            br = new BufferedReader(new InputStreamReader(fis));            String line = null;            while ((line=br.readLine())!=null){                // 每次读取一行并将其拼接起来                sb.append(line);            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if(br!=null){                try {                    // 关闭缓冲读取器                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return sb.toString();    }
public void myClick(View view) {        // 获取sd卡数据大小        File file = Environment.getExternalStorageDirectory();        long totalSpace = file.getTotalSpace();        long usableSpace = file.getUsableSpace();        // import android.text.format.Formatter;        String s = Formatter.formatFileSize(this, totalSpace);        String s1 = Formatter.formatFileSize(this, usableSpace);        Log.e("总大小", "=" + s);        Log.e("可用空间", "=" + s1);    }

这里写图片描述

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 被银行冻结的钱怎么办 蚂蚁花呗冻结了怎么办 淘宝号被冻结了怎么办 qq钱包被冻结了怎么办 浦发信用卡降额怎么办 外地邮政卡丢了怎么办 手机掉了,支付宝怎么办 支付宝手机丢了怎么办 手机丢了,微信怎么办 手机丢后支付宝怎么办 oppo手机被偷了怎么办 三星手机被偷了怎么办 手机被偷了号码怎么办 手机被偷 微信怎么办 手机被偷怎么办,还关机 我的手机被偷了怎么办 医保卡不能用了怎么办 招行u盾丢了怎么办 信用卡刷爆了要怎么办 办分期的卡丢了怎么办 房贷银行卡掉了怎么办 房贷银行卡丢了怎么办? 行驶证丢了怎么办补办 行驶证丢了怎么办异地 浦发信用卡盗刷怎么办 苹果id被盗变砖怎么办 信用卡丢了被刷怎么办 ins注册不了怎么办安卓 偷玩电脑被发现怎么办 做作业不认真的怎么办 老人脑供血不足怎么办 哺乳期吃了辣的怎么办 孕32周胎位臀位怎么办 怀孕32周胎位不正怎么办 7个月胎位不正怎么办 胎心监护老不过怎么办 8个月了胎位不正怎么办 怀孕八个月了胎位不正怎么办 怀孕八个月胎位不正怎么办 欠债的人跑了怎么办 赌博输了100万怎么办