IOS中计算缓存文件的大小判断实例详解
IOS中计算缓存文件的大小判断实例详解
IOS中计算缓存文件的大小判断,在这里分享一下自己的心得,希望和大家一起分享技术,如果有什么不足,还请大家指正。写出这篇目的,就是希望大家一起成长,我也相信技术之间没有高低,只有互补,只有分享,才能使彼此更加成长。
实例代码:
//获取缓存文件路径
-(NSString*)getCachesPath{
//获取Caches目录路径
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString*cachesDir=[pathsobjectAtIndex:0];
NSString*filePath=[cachesDirstringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];
returnfilePath;
}
///计算缓存文件的大小的M
-(longlong)fileSizeAtPath:(NSString*)filePath{
NSFileManager*manager=[NSFileManagerdefaultManager];
if([managerfileExistsAtPath:filePath]){
////取得一个目录下得所有文件名
//NSArray*files=[managersubpathsAtPath:filePath];
//NSLog(@"files1111111%@==%ld",files,files.count);
//
////从路径中获得完整的文件名(带后缀)
//NSString*exe=[filePathlastPathComponent];
//NSLog(@"exeexe====%@",exe);
//
////获得文件名(不带后缀)
//exe=[exestringByDeletingPathExtension];
//
////获得文件名(不带后缀)
//NSString*exestr=[[filesobjectAtIndex:1]stringByDeletingPathExtension];
//NSLog(@"files2222222%@====%@",[filesobjectAtIndex:1],exestr);
return[[managerattributesOfItemAtPath:filePatherror:nil]fileSize];
}
return0;
}
-(float)folderSizeAtPath:(NSString*)folderPath{
NSFileManager*manager=[NSFileManagerdefaultManager];
if(![managerfileExistsAtPath:folderPath])return0;
NSEnumerator*childFilesEnumerator=[[managersubpathsAtPath:folderPath]objectEnumerator];//从前向后枚举器//////
NSString*fileName;
longlongfolderSize=0;
while((fileName=[childFilesEnumeratornextObject])!=nil){
NSLog(@"fileName====%@",fileName);
NSString*fileAbsolutePath=[folderPathstringByAppendingPathComponent:fileName];
NSLog(@"fileAbsolutePath====%@",fileAbsolutePath);
folderSize+=[selffileSizeAtPath:fileAbsolutePath];
}
NSLog(@"folderSize====%lld",folderSize);
returnfolderSize/(1024.0*1024.0);
}
////////////
-(void)ss{
//获取Caches目录路径
NSArray*paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
NSString*cachesDir=[pathsobjectAtIndex:0];
NSLog(@"cachesDircachesDir==%@",cachesDir);
//读取缓存里面的具体单个文件/或全部文件//
NSString*filePath=[cachesDirstringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];
NSArray*array=[[NSArrayalloc]initWithContentsOfFile:filePath];
NSLog(@"filePathfilePath%@==array====%@",filePath,array);
NSFileManager*fm=[NSFileManagerdefaultManager];
if([fmfileExistsAtPath:filePath]){
//取得一个目录下得所有文件名
NSArray*files=[fmsubpathsAtPath:filePath];
NSLog(@"files1111111%@==%ld",files,files.count);
//获得文件名(不带后缀)
NSString*exestr=[[filesobjectAtIndex:1]stringByDeletingPathExtension];
NSLog(@"files2222222%@====%@",[filesobjectAtIndex:1],exestr);
}
}
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!