本地文件缓存工具类

public class CacheUtil {

/**
* 获取文件缓存数据的大小
*/
public static String getFileCacheSize(String imagePath,String soundPath) {
float cacheSize = 0;
String cacheSizeStr = "";
File imageFile = new File(imagePath);
File soundFile = new File(soundPath);
File[] imageF;
File[] soundF;
if (imageFile.exists()) {
imageF = imageFile.listFiles();

for (int i = 0; i < imageF.length; i++) {
cacheSize += imageF[i].length();
}
}
if (soundFile.exists()) {
soundF = soundFile.listFiles();

for (int i = 0; i < soundF.length; i++) {
cacheSize += soundF[i].length();
}
}
cacheSize = cacheSize / 1024 / 1024;
DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
cacheSizeStr = decimalFormat.format(cacheSize);//format 返回的是字符串
return cacheSizeStr;
}

/**
* 清空文件缓存
*/
public static void clearFileCache(String imagePath,String soundPath){
File imageFile = new File(imagePath);
File soundFile = new File(soundPath);
File[] imageF = imageFile.listFiles();
File[] soundF = soundFile.listFiles();
if (imageFile.exists()) {
for (int i = 0; i < imageF.length; i++) {
clear(imageF[i]);
}
}
if (soundFile.exists()) {
for (int i = 0; i < soundF.length; i++) {
clear(soundF[i]);
}
}
}

/**
* 若将整个ttpod文件夹删除,则只需调用这个方法
*/
public static void clear(File file) {
if (file.exists()) { //指定文件是否存在
if (file.isFile()) { //该路径名表示的文件是否是一个标准文件
file.delete(); //删除该文件
} else if (file.isDirectory()) { //该路径名表示的文件是否是一个目录(文件夹)
File[] files = file.listFiles(); //列出当前文件夹下的所有文件
for (File f : files) {
clear(f); //递归删除
}
}
file.delete(); //删除文件夹(song,art,lyric)
}
}
}

转载于:https://www.cnblogs.com/zhangminghan/p/5976275.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值