首页 > 系统 > iOS > 正文

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

2020-02-19 15:45:29
字体:
来源:转载
供稿:网友

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);  }}

根据武林技术频道小编的介绍IOS中计算缓存文件的大小判断实例详解,大家能记住了吧!如果你想找靠谱的平台可以上js.Vevb.com收藏。

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