获取文件尺寸清除缓存
来源:互联网 发布:淘宝怎么开充值店 编辑:程序博客网 时间:2024/06/05 07:31
1. 计算文件尺寸
//获取cache文件夹的路径
NSString *cachePath = directoryPath;
// 创建文件管理者
NSFileManager *manager = [NSFileManagerdefaultManager];
// 判断是否是文件夹
BOOL isDirectory = NO;
// 判断当前路径的文件是否存在
BOOL isExists = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
// 如果文件不存在或者不是文件夹
if (!isExists || !isDirectory) {
@throw [NSExceptionexceptionWithName:@"fileError"reason:@"不是文件夹路径,或者当前文件夹不存在"userInfo:nil];
}
// 获取文件夹所有子路径数组(缓存文件),可以获取多级目录下文件路径
NSArray *subpaths = [manager subpathsAtPath:cachePath];
//定义变量保存文件的尺寸
int totalSize = 0;
// 遍历文件路径
for (NSString *subPathin subpaths) {
NSString *filePath = [cachePath stringByAppendingPathComponent:subPath];
// 如果是隐藏文件夹,就跳过
if ([filePath containsString:@".DS"]) continue;
// 判断是否是文件夹
BOOL isDirectory = NO;
// 判断当前路径的文件是否存在
BOOL isExists = [manager fileExistsAtPath:filePath isDirectory:&isDirectory];
// 如果文件不存在或者是一个文件夹,就跳过
if (!isExists || isDirectory) continue;
// 获取文件的属性
NSDictionary *arrt = [manager attributesOfItemAtPath:filePath error:nil];
// 计算总尺寸
totalSize += [arrt fileSize];
}
}
// 如果指定了一个不存在的文件夹如何处理
// 创建文件管理者
NSFileManager *mgr = [NSFileManagerdefaultManager];
// 判断是否是文件夹
BOOL isDirector = NO;
// 判断当前路径的文件是否存在
BOOL isExists = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirector];
if (!isExists || !isDirector) {
// 抛异常
@throw [NSExceptionexceptionWithName:@"fillError"reason:@"不是文件夹路径,或者文件夹不存在"userInfo:nil];
}
// 清除文件夹
[[NSFileManagerdefaultManager] removeItemAtPath:directoryPatherror:nil];
// 根据路径创建新的文件夹
[[NSFileManagerdefaultManager] createDirectoryAtPath:directoryPathwithIntermediateDirectories:YESattributes:nilerror:nil];
3. 实现传入一个文件路径就能够获取文件尺寸的字符串
#import <Foundation/Foundation.h>@interface CQFileManager : NSFileManager/** 指定一个文件夹路径,获取当前文件夹路径尺寸 */+ (void)getDirectorySize:(NSString *)directoryPath completion:(void(^)(int totalSize))completion;/** * 指定一个文件夹路径,删除这个文件夹 */+ (void)removeDirectoryPath:(NSString *)directoryPath;/** * 指定一个文件夹路径,获取当前文件夹尺寸字符串 */+ (void)directorySizeString:(NSString *)directoryPath compltion:(void(^)(NSString *directorySize))strCompletion;@end
#import "CQFileManager.h"@implementation CQFileManager// 获取指定路径文件夹尺寸字符串+ (void)directorySizeString:(NSString *)directoryPath compltion:(void (^)(NSString *directorySize))strCompletion{ [CQFileManager getDirectorySize:directoryPath completion:^(int totalSize) { NSString *str = @"清除缓存"; // 尺寸 NSInteger size = totalSize; if (size > 1000 * 1000) { CGFloat fileSize = size / 1000.0 / 1000.0; str = [NSString stringWithFormat:@"%@(%0.1fMB)",str, fileSize]; } else if (size > 1000) { CGFloat fileSize = size / 1000.0; str = [NSString stringWithFormat:@"%@(%0.1fKB)",str, fileSize]; } else if (size > 0) { str = [NSString stringWithFormat:@"%@(%ldB)",str, size]; } str = [str stringByReplacingOccurrencesOfString:@".0" withString:@""]; if (strCompletion) { strCompletion(str); } }];}// 获取指定路径文件夹尺寸+ (void)getDirectorySize:(NSString *)directoryPath completion:(void (^)(int totalSize))completion{ // 在子线程中计算尺寸 dispatch_async(dispatch_get_global_queue(0, 0), ^{ // 获取cache文件夹的路径 NSString *cachePath = directoryPath; // 创建文件管理者 NSFileManager *manager = [NSFileManager defaultManager]; // 判断是否是文件夹 BOOL isDirectory = NO; // 判断当前路径的文件是否存在 BOOL isExists = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory]; // 如果文件不存在或者不是文件夹 if (!isExists || !isDirectory) { @throw [NSException exceptionWithName:@"fileError" reason:@"不是文件夹路径, 或者当前文件夹不存在" userInfo:nil]; } // 获取文件夹所有子路径数组(缓存文件),可以获取多级目录下文件路径 NSArray *subpaths = [manager subpathsAtPath:cachePath]; // 定义变量保存文件的尺寸 int totalSize = 0; // 遍历文件路径 for (NSString *subPath in subpaths) { NSString *filePath = [cachePath stringByAppendingPathComponent:subPath]; // 如果是隐藏文件夹,就跳过 if ([filePath containsString:@".DS"]) continue; // 判断是否是文件夹 BOOL isDirectory = NO; // 判断当前路径的文件是否存在 BOOL isExists = [manager fileExistsAtPath:filePath isDirectory:&isDirectory]; // 如果文件不存在或者是一个文件夹,就跳过 if (!isExists || isDirectory) continue; // 获取文件的属性 NSDictionary *arrt = [manager attributesOfItemAtPath:filePath error:nil]; // 计算总尺寸 totalSize += [arrt fileSize]; } // 回到主线程将文件尺寸转换成字符串并显示 dispatch_sync(dispatch_get_main_queue(), ^{ if (completion) { completion(totalSize); } }); });}// 删除指定路径文件夹+ (void)removeDirectoryPath:(NSString *)directoryPath{ // 如果指定了一个不存在的文件夹如何处理 // 创建文件管理者 NSFileManager *mgr = [NSFileManager defaultManager]; // 判断是否是文件夹 BOOL isDirector = NO; // 判断当前路径的文件是否存在 BOOL isExists = [mgr fileExistsAtPath:directoryPath isDirectory:&isDirector]; if (!isExists || !isDirector) { // 抛异常 @throw [NSException exceptionWithName:@"fillError" reason:@"不是文件夹路径,或者文件夹不存在" userInfo:nil]; } // 清除文件夹 [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil]; // 根据路径创建新的文件夹 [[NSFileManager defaultManager] createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil]; }@end
#import "CQSettingViewController.h"#import "CQFileManager.h"//目标文件路径的宏#define CachePath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]@interface CQSettingViewController ()@property (nonatomic, strong) NSString *str;@end@implementation CQSettingViewController- (void)viewDidLoad { [super viewDidLoad]; [self calculateCacheSize]; }- (void)calculateCacheSize{ // 获取当前缓存字符串 [CQFileManager directorySizeString:CachePath compltion:^(NSString *directorySize) { _str = directorySize; [self.tableView reloadData]; }];}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [CQFileManager removeDirectoryPath:CachePath]; // 刷新后没有修改数据是因为没有重新计算缓存大小, 所以刷新后显示的尺寸还是之前保存的str;// [self.tableView reloadData]; [self calculateCacheSize];}
- 获取文件尺寸清除缓存
- iOS获取缓存文件的大小并清除缓存
- 缓存获取与清除
- SDWebImage 获取缓存,清除缓存
- [Object-c]获取文件的大小 清除缓存
- 清除文件缓存
- 清除缓存文件
- 清除缓存文件
- ThinkPHP清除文件缓存
- 清除xcode 缓存文件
- 清除缓存文件
- 文件清除缓存
- ios-清除文件缓存
- 获取版本号和清除缓存
- Android缓存获取与清除
- 获取缓存大小和清除缓存功能
- 获取缓存大小和清除缓存功能
- iOS 获取缓存大小 清除缓存
- 网络编程基础(2)-协议概要-TCP首部
- java虚拟机栈和本地方法栈溢出
- c++ 中用typedef定义新类型(类对象)小例子
- 封装:实现单例、多例模式,业务层继承调用
- Java 后台实现上传图片的接收并保存
- 获取文件尺寸清除缓存
- js去掉html标签和去掉字符串文本的所有的空格
- jedis命令综合(2)
- Debian及树莓派改时间
- 浅谈SQL SERVER中事务的ACID
- Android_adb_adb opendir failed ,permission denied的解决方法
- 264. Ugly Number II
- Arm+linux+qt 应用调试技巧
- android 单点登录