oc 根据文件路径获取文件大小
第一种封装:
-(NSInteger)getSizeOfFilePath:(NSString *)filePath{
/** 定义记录大小 */
NSInteger totalSize = ;
/** 创建一个文件管理对象 */
NSFileManager * manager = [NSFileManager defaultManager];
/**获取文件下的所有路径包括子路径 */
NSArray * subPaths = [manager subpathsAtPath:filePath];
/** 遍历获取文件名称 */
for (NSString * fileName in subPaths) {
/** 拼接获取完整路径 */
NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
/** 判断是否是隐藏文件 */
if ([fileName hasPrefix:@".DS"]) {
continue;
}
/** 判断是否是文件夹 */
BOOL isDirectory;
[manager fileExistsAtPath:subPath isDirectory:&isDirectory];
if (isDirectory) {
continue;
}
/** 获取文件属性 */
NSDictionary *dict = [manager attributesOfItemAtPath:subPath error:nil];
/** 累加 */
totalSize += [dict fileSize];
}
/** 返回 */
return totalSize;
}
第二种 block:
/** 根据文件路径删除文件 */
+(void)removeDirectoryPath:(NSString *)directoryPath{ /** 创建文件管理者 */
NSFileManager * manager = [NSFileManager defaultManager];
/** 判断文件路径是否存在 */
BOOL isDirectory;
BOOL isExist = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (!isDirectory||!isExist) {
/** 提示错误信息 */
@throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路径错误!" userInfo:nil];
}
/** 删除文件 */
[manager removeItemAtPath:directoryPath error:nil];
/** 创建文件 */
[manager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
}
/** 根据文件路径获取文件大小 */
+(void)getSizeOfFilePath:(NSString *)filePath completion:(void (^)(NSInteger totalSize))completion{ /** 创建文件管理者 */
NSFileManager * manager = [NSFileManager defaultManager];
/** 判断文件路径是否存在 */
BOOL isDirectory;
BOOL isExist = [manager fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isDirectory||!isExist) {
/** 提示错误信息 */
@throw [NSException exceptionWithName:NSStringFromClass(self) reason:@"文件路径错误!" userInfo:nil];
}
/** 开启子线程因以下是耗时操作 */
dispatch_async(dispatch_get_global_queue(, ), ^{
/** 定义记录文件大小 */
NSInteger totalSize = ; /** 获取文件 */
NSArray * subPaths = [manager subpathsAtPath:filePath];
/** 遍历文件 */
for (NSString * fileName in subPaths) {
/** 拼接完整路径 */
NSString * subPath = [filePath stringByAppendingPathComponent:fileName];
/** 判断是否是隐藏.DS_Store */
if ([subPath hasPrefix:@".DS"]) {
continue;
}
/** 判断是否是文件夹 */
BOOL isDirectory;
[manager fileExistsAtPath:subPath isDirectory:&isDirectory];
if (isDirectory) {
continue;
}
/** 获取文件属性 */
NSDictionary * dic = [manager attributesOfItemAtPath:subPath error:nil];
totalSize += [dic fileSize];
}
/** 回到主线程 */
dispatch_sync(dispatch_get_main_queue(), ^{
/** block不为空 */
if (completion) {
completion(totalSize);
}
});
}); }
oc 根据文件路径获取文件大小的更多相关文章
- OC NSFileManager(文件路径操作)
OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...
- [WinAPI] API 10 [创建、打开、读写文件,获取文件大小]
在Windows系统中,创建和打开文件都是使用API函数CreateFile,CreateFile通过指定不同的参数来表示是新建一个文件,打开已经存在的文件,还是重新建立文件等.读写文件最为直接的方式 ...
- c++从文件路径获取目录
场景 c++从文件路径获取目录 实现代码 初始化是不正确的,因为需要转义反斜杠: string filename = "C:\\MyDirectory\\MyFile.bat"; ...
- 【原创】ABAP根据文件路径获取文件所在目录(续)
在上一篇文章<ABAP根据文件路径获取文件所在目录>中,我主要的思路是采用 “SPLIT dobj AT sep INTO TABLE result_tab” 句型将文件全路径按分隔符“\ ...
- C# Path 有关于文件路径获取的问题 的方法
string Current = Directory.GetCurrentDirectory();//获取当前根目录 //private string strFilePath = Applicatio ...
- ios 关于文件操作 获取 文件大小
分类: Apple IPhone2012-06-28 11:31 4664人阅读 评论(0) 收藏 举报 ios语言manager测试c c语言 实现 #include "sys/stat ...
- java项目部署后的文件路径获取
//eclipse部署工程 String path = request.getServletContext().getRealPath( File.separator+ "WEB-INF&q ...
- 【原创】ABAP根据文件路径获取文件所在目录
*&---------------------------------------------------------------------* *& Form frm_get_pat ...
- 设置下载文件路径 & 获取接口结尾名称。
// 获取下载位置 private String isExistDir(String saveDir) throws IOException { File downloadFile = new Fil ...
随机推荐
- NODE.JS安装配置
- linux之SQL语句简明教程---ALTER TABLE
在表格被建立在资料库中后,我们常常会发现,这个表格的结构需要有所改变.常见的改变如下: 加一个栏位 删去一个栏位 改变栏位名称 改变栏位的资料种类 以上列出的改变并不是所有可能的改变.ALTER TA ...
- 网易云课堂_C++开发入门到精通_章节3: 类、对象和封装
课时12构造函数与析构函数-2 构造函数 构造函数可以有多个 构造函数可以重载 构造函数用于隐式类型转换 class Student { public: explicit Student(int ss ...
- CvMat、Mat、IplImage之间的转换详解及实例
见原博客:http://blog.sina.com.cn/s/blog_74a459380101obhm.html OpenCV学习之CvMat的用法详解及实例 CvMat是OpenCV比较基础的函数 ...
- ShareSDK QQ分享失败的坑
QQ分享的话,有标题和内容字符数限制,QQ好友的话限制的很小,标题30字符,内容40字符.分享之前限制一下.
- POJ 2420 A Star not a Tree? (计算几何-费马点)
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
- 使用 hibernate 根据映射文件生成数据库表
为了更好的显示效果,可以在hibernate.cfg.xml配置文件的<session-factory>标签里加入以下内容: 显示sql语句和格式化显示sql语句: <propert ...
- 5.7 cm server-agent 会出现无法启动
异常信息如下: 离线安装cloudera-scm-agent5.7的Unable to create the pidfile问题 在离线安装Cloudera Manager启动agent出现了如下异常 ...
- eclipse 各种版本spring插件安装
一.安装之前先得知道怎么下载吧: 一般网上找到的资料是到这里就GG了的 --> http://spring.io/tools/sts/all 很明显这点版本是不够我们需要的,假如我们的eclip ...
- TCP/IP详解之:UDP协议
第11章 UDP协议 UDP首部 UDP的检验和是可选的,而TCP的检验和是必须的: UDP的检验和是端到端的检验和.由发送端计算,由接收端验证: 尽管UDP的检验和是可选的,但总是推荐被使用 IP ...