读取与写入Cache

本文介绍了一个用于Android平台的文件读写工具类,该工具类能够根据设备情况选择使用SD卡或应用缓存目录进行文件操作,并包含了读取和写入缓存的具体实现。

分析

  1. 配置读写权限
  2. 写文件工具类,主要实现获得指定路径的字符串,例如缓存或图片文件夹
    1. 判断是否存在SD卡
    2. 如果有,则指定一条绝对路径,例如( path = /sd/ReadTest/”str”)
    3. 如果没有,则指定项目安装包下的缓存,例如(Paht = /data/data/项目安装包/cache)
  3. 根据指定路径与文件名称,构建读写文件
  4. 根据读写文件,构建读写文件流
  5. 根据读写文件流,读文件成字符串或写字符串到指定文件

配置

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

工具类

// 获得上下文环境
public class BaseApplication extends Application {
    public static BaseApplication application;

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
    }
    public static BaseApplication getApplication(){
        return  application;
    }
}
// 文件工具类,主要用于获得指定文件夹字符地址
public class FileUtils {
    private static String TAG = "ReadTest";
    private static String CACHE = "cache";
    // 获得str文件夹
    public static String getDir(String str){
        String path = "";
        if (isSDAvail()){
            // path = /sd/ReadTest/"str"
            String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            path += sdPath;
            path += "/";
            path += TAG;
            path += "/";
            path += str;
        }else {
            // Paht = /data/data/项目安装包/cache
            File cashe = ContextUtils.GetContext().getCacheDir();
            path = cashe.getAbsolutePath();
        }
        File file = new File(path.toString());
        if (! file.exists() || !file.isDirectory()){
            file.mkdir();
        }
        return path;
    }
    // 判断是否存在SD卡
    public static boolean isSDAvail() {
        // 是否有SD挂载
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            return true;
        }
        return false;
    }
    // 获得缓存文件夹
    public static String getCacheDir(){
        return getDir(CACHE);
    }
}

读取Cache

private String loadCache(String url) {
        // 获得缓存的指定位置
        String cashe = FileUtils.getCacheDir();
        File dir = new File(cashe);
        // 获得url的最后文件名称;
        int i = url.lastIndexOf("/");
        String fileName = url.substring(i, url.length());
        // 在指定的路劲dir下,存放的文件名称fileName;
        File file = new File(dir, fileName);
        // 读取数据流
        BufferedReader br = null;
        try {
            // 读取文件
            FileReader fr = new FileReader(file);
            br = new BufferedReader(fr);
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null){
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

写入Cache

private void saveCache(String url, String res) {
        // 获得缓存的指定位置
        String cache  = FileUtils.getCacheDir();
         // 获得url的最后文件名称;
        File dir = new File(cache);
        int i = url.lastIndexOf("/");
        String fileName = url.substring(i, url.length());
         // 在指定的路劲dir下,存放的文件名称fileName;
        File file = new File(dir,fileName);
        // 写入数据流
        BufferedWriter bw = null;
        try {
            FileWriter fw = new FileWriter(file);
            bw = new BufferedWriter(fw);
            bw.write(res);
            bw.flush();
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值