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

时间:2022-09-19 17:18:48

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

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

实例代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//获取缓存文件路径
-(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);
  }
 
}

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

原文链接:http://blog.csdn.net/yishengzhiai005/article/details/72821579