新浪微博客户端(62)-计算某个文件或文件夹的大小
2016-12-19 20:58
323 查看
NSString+Extension.m
移除某个文件或文件夹:
最终效果:
/** * 计算当前路径字符串指定的文件/文件夹大小 * 文件/文件夹大小,-1代表指定的文件或文件夹路径不存在,以字节为单位 */ - (NSInteger)fileSize { NSFileManager *mgr = [NSFileManager defaultManager]; BOOL dir = NO; BOOL exists = [mgr fileExistsAtPath:self isDirectory:&dir]; if (!exists) { return -1; } if (dir) { // 目录 NSInteger totalByteSize = 0; NSArray *subPaths = [mgr subpathsAtPath:self]; // 单个文件路径 for (NSString *subPath in subPaths) { // 计算完整路径下的文件大小,并累加 totalByteSize += [[mgr attributesOfItemAtPath:[self stringByAppendingPathComponent:subPath] error:nil][NSFileSize] integerValue]; } return totalByteSize; } else { // 文件 return [[mgr attributesOfItemAtPath:self error:nil][NSFileSize] integerValue] ; } }
移除某个文件或文件夹:
// 清除缓存 - (void)clearCache { NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSFileManager *mgr = [NSFileManager defaultManager]; [mgr removeItemAtPath:cachePath error:nil]; }
最终效果:
相关文章推荐
- 控制反转与依赖注入
- MyBatis连接数据库
- 在Spring MVC框架下配置双(多)数据源
- UVa 232 - Crossword Answers
- 操作系统之进程的状态
- ECMAScript6(ES6)标准之数组Array扩展方法
- System.currentTimeMillis()笔记
- 通过监听器实现在线人数统计
- JVM 学习 第 2 课 虚拟机class文件从哪里寻找?哪里来?
- [C#]DataGridView自动调整列宽
- 初学activemq 关键字识别与介绍
- Python IO 编程
- mac os系统复制粘贴ctrl+c ctrl+v快捷键实现方式
- 解析XML格式数据--Pull解析
- java中使用队列:java.util.Queue (转)
- Codeforces Round #387 (Div. 2) C && coedeforces 744 C Servers(水题)
- 利用Python从数据公司接口获取数据并输出excel格式
- poj 1127 简单几何+并查集
- ubuntu 安装搜狗输入法(解决部分ubuntu安装完没有键盘选择栏)
- PHP设计模式(七)适配器模式(Adapter For PHP)