首页 > 系统 > iOS > 正文

IOS中计算缓存文件的大小判断实例详解

2020-07-26 02:40:31
字体:
来源:转载
供稿:网友

IOS中计算缓存文件的大小判断实例详解

IOS中计算缓存文件的大小判断在这里分享一下自己的心得,希望和大家一起分享技术,如果有什么不足,还请大家指正。写出这篇目的,就是希望大家一起成长,我也相信技术之间没有高低,只有互补,只有分享,才能使彼此更加成长。 

实例代码:

//获取缓存文件路径-(NSString *)getCachesPath{  // 获取Caches目录路径  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);  NSString *cachesDir = [paths objectAtIndex:0];    NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];    return filePath;}///计算缓存文件的大小的M- (long long) fileSizeAtPath:(NSString*) filePath{  NSFileManager* manager = [NSFileManager defaultManager];  if ([manager fileExistsAtPath:filePath]){    //    //取得一个目录下得所有文件名//    NSArray *files = [manager subpathsAtPath:filePath];//    NSLog(@"files1111111%@ == %ld",files,files.count);//    //    // 从路径中获得完整的文件名(带后缀)//    NSString *exe = [filePath lastPathComponent];//    NSLog(@"exeexe ====%@",exe);//    //    // 获得文件名(不带后缀)//    exe = [exe stringByDeletingPathExtension];//    //    // 获得文件名(不带后缀)//    NSString *exestr = [[files objectAtIndex:1] stringByDeletingPathExtension];//    NSLog(@"files2222222%@ ==== %@",[files objectAtIndex:1],exestr);            return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];  }    return 0;}- (float ) folderSizeAtPath:(NSString*) folderPath{  NSFileManager* manager = [NSFileManager defaultManager];  if (![manager fileExistsAtPath:folderPath]) return 0;  NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];//从前向后枚举器//////  NSString* fileName;  long long folderSize = 0;  while ((fileName = [childFilesEnumerator nextObject]) != nil){    NSLog(@"fileName ==== %@",fileName);    NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];    NSLog(@"fileAbsolutePath ==== %@",fileAbsolutePath);    folderSize += [self fileSizeAtPath:fileAbsolutePath];  }  NSLog(@"folderSize ==== %lld",folderSize);  return folderSize/(1024.0*1024.0);}////////////-(void)ss{  // 获取Caches目录路径  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES);  NSString *cachesDir = [paths objectAtIndex:0];    NSLog(@"cachesDircachesDir == %@",cachesDir);  //读取缓存里面的具体单个文件/或全部文件//  NSString *filePath = [cachesDir stringByAppendingPathComponent:@"com.nickcheng.NCMusicEngine"];  NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];  NSLog(@"filePathfilePath%@ ==array==== %@",filePath, array);      NSFileManager* fm=[NSFileManager defaultManager];  if([fm fileExistsAtPath:filePath]){    //取得一个目录下得所有文件名    NSArray *files = [fm subpathsAtPath:filePath];    NSLog(@"files1111111%@ == %ld",files,files.count);        // 获得文件名(不带后缀)    NSString * exestr = [[files objectAtIndex:1] stringByDeletingPathExtension];    NSLog(@"files2222222%@ ==== %@",[files objectAtIndex:1],exestr);  }}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本文能帮助到大家,谢谢大家对本站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表