首页 > 代码库 > 封装读取文件路径的类File.h+File.m

封装读取文件路径的类File.h+File.m

 1 #import <Foundation/Foundation.h> 2  3 #define FILE_PATH(filePath)       [File path:(filePath)] 4 #define ROOT_PATH                 [File rootPath] 5 #define BUNDLE_PATH(fileName)     [File bundleSource:(fileName)] 6 #define CREATE_FOLDER(folderPath) [File createFolder:(folderPath)] 7 #define FILE_EXIST(filePath)      [File exist:(filePath)] 8 #define IS_DIRECTORY(filePath)    [File isDirectory:(filePath)] 9 #define IS_FILE(filePath)         ![File isDirectory:(filePath)]10 #define FILE_INFO(filePath)       [File fileInfo:(filePath)]11 #define FILE_SIZE(filePath)       [File fileSize:(filePath)]12 #define FILE_DICTIONARY(filePath) [File dictionaryFrom:(filePath)]13 #define FILE_ARRAY(filePath)      [File arrayFrom:(filePath)]14 15 @interface File : NSObject16 17 /*18  注意:凡是参数为 filePath folderPath 的全部为相对路径,用以下字符串拼接使用19  20  /Documents21  /Library/Caches22  /Library/Preferences23  /tmp24  25  其他都为绝对路径26  */27 28 + (NSString *)rootPath;29 + (NSString *)bundleSource:(NSString *)fileName;30 + (NSString *)path:(NSString *)filePath;31 + (BOOL)createFolder:(NSString *)filePath;32 + (BOOL)exist:(NSString *)filePath;33 + (BOOL)isDirectory:(NSString *)filePath;34 + (NSDictionary *)fileInfo:(NSString *)filePath;35 + (int)fileSize:(NSString *)filePath;36 + (NSArray *)enumeratorFolder:(NSString *)folderPath;37 + (void)enumeratorFolder:(NSString *)folderPath each:(void (^)(NSString *path))each;38 39 + (BOOL)copyFrom:(NSString *)sourcePath to:(NSString *)targetPath;40 + (BOOL)moveFrom:(NSString *)sourcePath to:(NSString *)targetPath;41 + (BOOL)remove:(NSString *)targetPath;42 43 + (BOOL)writePlist:(id)plist to:(NSString *)filePath;44 + (NSMutableDictionary *)dictionaryFrom:(NSString *)filePath;45 + (NSMutableArray *)arrayFrom:(NSString *)filePath;46 + (void)path:(NSString *)filePath dictionary:(void (^)(NSMutableDictionary *dictionary))dictionary;47 + (void)path:(NSString *)filePath array:(void (^)(NSMutableArray *array))array;48 49 @end

File.m代码如下:

  1 #import "File.h"  2   3 static NSString *_sandBoxPath = nil;  4   5 @implementation File  6   7 + (void)initialize  8 {  9     if (self == [File class]) 10     { 11         _sandBoxPath = NSHomeDirectory(); 12     } 13 } 14  15 + (NSString *)rootPath 16 { 17     return _sandBoxPath; 18 } 19  20 + (NSString *)path:(NSString *)filePath 21 { 22     return [_sandBoxPath stringByAppendingPathComponent:filePath]; 23 } 24  25 + (BOOL)createFolder:(NSString *)filePath 26 { 27     return [[NSFileManager defaultManager] createDirectoryAtPath:[self path:filePath] 28                                      withIntermediateDirectories:YES 29                                                       attributes:nil 30                                                            error:nil]; 31 } 32  33 + (NSString *)bundleSource:(NSString *)fileName 34 { 35     return [[NSBundle mainBundle] pathForResource:fileName 36                                            ofType:nil]; 37 } 38  39 + (BOOL)exist:(NSString *)filePath 40 { 41     return [[NSFileManager defaultManager] fileExistsAtPath:[self path:filePath] 42                                                 isDirectory:NO]; 43 } 44  45 + (BOOL)isDirectory:(NSString *)filePath 46 { 47     BOOL isDirectory = NO; 48      49     [[NSFileManager defaultManager] fileExistsAtPath:[self path:filePath] 50                                          isDirectory:&isDirectory]; 51      52     return isDirectory; 53 } 54  55 + (BOOL)copyFrom:(NSString *)sourcePath to:(NSString *)targetPath 56 { 57     return [[NSFileManager defaultManager] copyItemAtPath:sourcePath 58                                                    toPath:targetPath 59                                                     error:nil]; 60 } 61  62 + (BOOL)moveFrom:(NSString *)sourcePath to:(NSString *)targetPath 63 { 64     return [[NSFileManager defaultManager] moveItemAtPath:sourcePath 65                                                    toPath:targetPath 66                                                     error:nil]; 67 } 68  69 + (BOOL)remove:(NSString *)targetPath 70 { 71     return [[NSFileManager defaultManager] removeItemAtPath:targetPath 72                                                       error:nil]; 73 } 74  75 + (NSArray *)enumeratorFolder:(NSString *)folderPath 76 { 77     if ([self isDirectory:folderPath]) 78     { 79         NSMutableArray *storeArray = [NSMutableArray array]; 80          81         NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:folderPath]; 82         NSFileManager *localFileManager = [[NSFileManager alloc] init]; 83         NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir]; 84          85         NSString *file; 86         while ((file = [dirEnum nextObject])) 87         { 88             [storeArray addObject:[folderPath stringByAppendingPathComponent:file]]; 89         } 90          91         return storeArray; 92     } 93     else 94     { 95         return nil; 96     } 97 } 98  99 + (void)enumeratorFolder:(NSString *)folderPath each:(void (^)(NSString *path))each100 {101     if ([self isDirectory:folderPath])102     {103         NSMutableArray *storeArray = [NSMutableArray array];104         105         NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:folderPath];106         NSFileManager *localFileManager = [[NSFileManager alloc] init];107         NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];108         109         NSString *file;110         while ((file = [dirEnum nextObject]))111         {112             [storeArray addObject:[folderPath stringByAppendingPathComponent:file]];113         }114         115         [storeArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {116             each(obj);117         }];118     }119 }120 121 + (NSDictionary *)fileInfo:(NSString *)filePath122 {123     return [[NSFileManager defaultManager] attributesOfItemAtPath:[self path:filePath]124                                                             error:nil];125 }126 127 + (int)fileSize:(NSString *)filePath128 {129     return [[[[NSFileManager defaultManager] attributesOfItemAtPath:[self path:filePath]130                                                               error:nil] 131              objectForKey:@"NSFileSize"] intValue];132 }133 134 + (BOOL)writePlist:(id)plist to:(NSString *)filePath135 {136     if ([plist isKindOfClass:[NSDictionary class]])137     {138         NSDictionary *point = plist;139         140         return [point writeToFile:[self path:filePath]141                        atomically:YES];142     }143     else if ([plist isKindOfClass:[NSArray class]])144     {145         NSArray *point = plist;146         147         return [point writeToFile:[self path:filePath]148                        atomically:YES];149     }150     else151     {152         return NO;153     }154 }155 156 + (NSMutableDictionary *)dictionaryFrom:(NSString *)filePath157 {158     NSMutableDictionary *dictionary = 159         [[NSMutableDictionary alloc] initWithContentsOfFile:[self path:filePath]];160     161     return dictionary;162 }163 164 + (NSMutableArray *)arrayFrom:(NSString *)filePath165 {166     NSMutableArray *array = 167         [[NSMutableArray alloc] initWithContentsOfFile:[self path:filePath]];168     169     return array;170 }171 172 + (void)path:(NSString *)filePath dictionary:(void (^)(NSMutableDictionary *dictionary))dictionary173 {174     NSMutableDictionary *sourceDictionary = 175         [[NSMutableDictionary alloc] initWithContentsOfFile:[self path:filePath]];176     177     dictionary(sourceDictionary);178     179     [sourceDictionary writeToFile:[self path:filePath]180                        atomically:YES];181 }182 183 + (void)path:(NSString *)filePath array:(void (^)(NSMutableArray *array))array184 {185     NSMutableArray *sourceArray = 186     [[NSMutableArray alloc] initWithContentsOfFile:[self path:filePath]];187     188     array(sourceArray);189     190     [sourceArray writeToFile:[self path:filePath]191                   atomically:YES];192 }193 194 @end