[关闭]
@fiy-fish 2015-07-19T18:10:54.000000Z 字数 2784 阅读 1472

day09-01-文件管理器练习

Objective-c


  1. // main.m
  2. // day09-01-文件管理器练习
  3. //
  4. // Created by Aaron on 15/7/13.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "MyFileManager.h"
  9. int main(int argc, const char * argv[]) {
  10. @autoreleasepool {
  11. MyFileManager *manager = [[MyFileManager alloc] init];
  12. [manager managerFileForPath:@"/Users/qianfeng/Desktop/action"];
  13. }
  14. return 0;
  15. }

  1. #import <Foundation/Foundation.h>
  2. @interface MyFileManager : NSObject
  3. -(void)managerFileForPath:(NSString *)path;
  4. @end

  1. #import "MyFileManager.h"
  2. /*
  3. 4.设计一个文件管理器
  4. 实现功能:
  5. 将dir目录下的所有文件分类管理
  6. dir目录下有各种后缀的文件 以及没有尾缀的文件
  7. 检索目录,按照后缀名创建子目录,把该后缀名的文件全丢进去
  8. 没有尾缀的创建一个other目录,把没有尾缀的文件全丢进去
  9. 不需要支持双尾缀
  10. 不用管理文件夹
  11. */
  12. /*
  13. 1.创建一个管理器类
  14. 2.遍历目录(浅遍历)
  15. 3.干掉文件夹
  16. 4.取出后缀,将文件名和后缀做关联
  17. 5.创建目录
  18. 6.移动文件
  19. */
  20. @interface MyFileManager ()
  21. @property (nonatomic,strong) NSMutableDictionary *sufAndFileDic;
  22. @end
  23. @implementation MyFileManager
  24. -(void)managerFileForPath:(NSString *)path
  25. {
  26. //创建文件管理器
  27. NSFileManager *manager = [NSFileManager defaultManager];
  28. //浅遍历目录
  29. NSArray *contents = [manager contentsOfDirectoryAtPath:path error:nil];
  30. //删除目录
  31. contents = [self deleteDirectoryInContents:contents withPath:path];
  32. //获取后缀
  33. NSArray *suffixs = [self suffixForFiles:contents];
  34. //利用后缀创建目录
  35. [self createDirectoryBySuffixs:suffixs withPath:path];
  36. //开始移动文件
  37. for(int i = 0; i < suffixs.count; i++)
  38. {
  39. //取后缀
  40. NSString *suf = suffixs[i];
  41. //取后缀对应的所有文件
  42. NSArray *files = [_sufAndFileDic allKeysForObject:suf];
  43. //遍历取出来的所有文件
  44. for(id obj in files)
  45. {
  46. [manager moveItemAtPath:[NSString stringWithFormat:@"%@/%@",path,obj] toPath:[NSString stringWithFormat:@"%@/%@/%@",path,suf,obj] error:nil];
  47. }
  48. }
  49. NSLog(@"%@",suffixs);
  50. }
  51. -(void)createDirectoryBySuffixs:(NSArray *)suffixs withPath:(NSString *)path
  52. {
  53. NSFileManager *manager = [NSFileManager defaultManager];
  54. for(NSString *obj in suffixs)
  55. {
  56. NSString *newPath = [NSString stringWithFormat:@"%@/%@",path,obj];
  57. [manager createDirectoryAtPath:newPath withIntermediateDirectories:NO attributes:nil error:nil];
  58. }
  59. }
  60. //获取后缀,并返回后缀的数组
  61. -(NSArray *)suffixForFiles:(NSArray *)files
  62. {
  63. NSMutableArray *array = [NSMutableArray array];
  64. if(!self.sufAndFileDic)
  65. {
  66. self.sufAndFileDic = [NSMutableDictionary dictionary];
  67. }
  68. for(NSString *obj in files)
  69. {
  70. //检查是否有.
  71. NSRange range = [obj rangeOfString:@"." options:NSBackwardsSearch];
  72. if(range.location != NSNotFound)
  73. {
  74. //有后缀
  75. NSString *suf = [obj substringFromIndex:range.location+1];
  76. if(![array containsObject:suf])
  77. {
  78. [array addObject:suf];
  79. }
  80. [_sufAndFileDic setObject:suf forKey:obj];
  81. }
  82. else
  83. {
  84. //没有后缀
  85. if(![array containsObject:@"other"])
  86. {
  87. [array addObject:@"other"];
  88. }
  89. [_sufAndFileDic setObject:@"other" forKey:obj];
  90. }
  91. }
  92. return array;
  93. }
  94. -(NSArray *)deleteDirectoryInContents:(NSArray *)array withPath:(NSString *)path
  95. {
  96. NSFileManager *manager = [NSFileManager defaultManager];
  97. BOOL isDir = NO;
  98. NSMutableArray *newContents = [NSMutableArray array];
  99. for(int i = 0; i < array.count; i++)
  100. {
  101. NSString *oneFile = array[i];
  102. [manager fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",path,oneFile] isDirectory:&isDir];
  103. if(!isDir)
  104. {
  105. [newContents addObject:oneFile];
  106. }
  107. }
  108. return newContents;
  109. }
  110. @end

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注