在過去的開發工作中,試了好多文件相關的類庫,都不能滿足工作中的需求。要麼是過於簡單,要麼是看似華麗,但實則空洞。最終,決定自己提供這樣一個類庫供大家使用,已簡化iOS開發中的文件操作。
HYFileManager一個基於NSFileManager的文件操作類,它提供一系列的靜態方法,只用少量的代碼,來處理經常需要處理的文件操作,使得工作更加方便快捷。當然,這並不是一個十全十美的類庫,只是一個工作經驗的積累,如果有任何問題,歡迎提issue,我會第一時間回復的。
在HYFileManager中,基本包含了日常開發中的絕大多數需求,包括一下幾種:
沙盒日常操作目錄集合,簡單語法即可獲取目錄路徑;
遍歷文件夾,包含兩種方式,深遍歷和淺遍歷;
獲取文件屬性,包含單個屬性獲取方法和所有屬性獲取方法;
創建文件(夾),創建文件夾,文件,可以根據所需,是否需要覆蓋,是否需要默認內容來創建;
刪除文件(夾),提供兩個靜態方法來快速實現清空Caches和tmp文件夾內容;
復制、移動文件(夾),可以選擇是否需要覆蓋來對文件(夾)進行復制和移動;
根據目錄路徑來獲取文件名和文件擴展類型;
判斷文件(夾)是否存在、文件(夾)判空、判斷路徑是否為文件或者文件夾、已經判斷目錄是否可讀可寫;
獲取文件(夾)的大小,提供兩種返回方式,NSNumber和NSString,方便開發使用;
寫入文件內容,支持基本數據類型、NSData、UIImage和NSCoding類型。
目前,HYFileManager已經支持CocoaPods,當然,也可以直接下載項目文件,拖到自己的工程裡即可。如果您有更好的優化建議,歡迎提供Pull request。
介紹下使用實例:
常見沙盒目錄
/* All shortcuts suppported: + (NSString *)homeDir; + (NSString *)documentsDir; + (NSString *)libraryDir; + (NSString *)preferencesDir; + (NSString *)cachesDir; + (NSString *)tmpDir; */ // 沙盒目錄 NSString *homePath = [HYFileManager homeDir];
遍歷文件夾
/* All shortcuts suppported: + (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep; + (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep; + (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep; + (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep; + (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep; + (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep; */ // 遍歷library文件夾 NSArray *libraryArr = [HYFileManager listFilesInLibraryDirectoryByDeep:NO];
獲取文件屬性
/* All shortcuts suppported: + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key; + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key error:(NSError **)error; + (NSDictionary *)attributesOfItemAtPath:(NSString *)path; + (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error; */ // 獲取文件創建時間 NSDate *date = (NSDate *)[HYFileManager attributeOfItemAtPath:path forKey:NSFileCreationDate error:error];
創建文件(夾)
/* All shortcuts suppported: + (BOOL)createDirectoryAtPath:(NSString *)path; + (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError **)error; + (BOOL)createFileAtPath:(NSString *)path; + (BOOL)createFileAtPath:(NSString *)path error:(NSError **)error; + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite; + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite error:(NSError **)error; + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content; + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite; + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite error:(NSError **)error; + (NSDate *)creationDateOfItemAtPath:(NSString *)path; + (NSDate *)creationDateOfItemAtPath:(NSString *)path error:(NSError **)error; + (NSDate *)modificationDateOfItemAtPath:(NSString *)path; + (NSDate *)modificationDateOfItemAtPath:(NSString *)path error:(NSError **)error; */ // library下創建一個test文件夾 NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]]; BOOL isSuccess = [HYFileManager createDirectoryAtPath:directoryPath];
刪除文件(夾)
/* All shortcuts suppported: + (BOOL)removeItemAtPath:(NSString *)path; + (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error; + (BOOL)clearCachesDirectory; + (BOOL)clearTmpDirectory; */ // 刪除library下的test文件夾 NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]]; BOOL isSuccess = [HYFileManager removeItemAtPath:directoryPath];
復制文件(夾)
/* All shortcuts suppported: + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath; + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; */ // 復制文件夾 NSError *error; NSString *path = [NSString stringWithFormat:@"%@/test/hyyy", [HYFileManager libraryDir]]; NSString *toPath = [NSString stringWithFormat:@"%@/hyyy", [HYFileManager libraryDir]]; BOOL isSuccess = [HYFileManager copyItemAtPath:path toPath:toPath overwrite:YES error:&error];
移動文件(夾)
/* All shortcuts suppported: + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath; + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; */ // 移動文件夾 NSError *error; NSString *path = [NSString stringWithFormat:@"%@/hyyy", [HYFileManager libraryDir]]; NSString *toPath = [NSString stringWithFormat:@"%@/test/hyyy", [HYFileManager libraryDir]]; BOOL isSuccess = [HYFileManager moveItemAtPath:path toPath:toPath overwrite:YES error:&error];
獲取文件名和擴展類型
/* All shortcuts suppported: + (NSString *)fileNameAtPath:(NSString *)path suffix:(BOOL)suffix; + (NSString *)directoryAtPath:(NSString *)path; + (NSString *)suffixAtPath:(NSString *)path; */ // 獲取文件夾名稱,帶後綴 NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; NSString *fileName = [HYFileManager fileNameAtPath:path suffix:YES];
文件(夾)是否存在、判空和可讀可寫
/* All shortcuts suppported: + (BOOL)isExistsAtPath:(NSString *)path; + (BOOL)isEmptyItemAtPath:(NSString *)path; + (BOOL)isEmptyItemAtPath:(NSString *)path error:(NSError **)error; + (BOOL)isDirectoryAtPath:(NSString *)path; + (BOOL)isDirectoryAtPath:(NSString *)path error:(NSError **)error; + (BOOL)isFileAtPath:(NSString *)path; + (BOOL)isFileAtPath:(NSString *)path error:(NSError **)error; + (BOOL)isExecutableItemAtPath:(NSString *)path; + (BOOL)isReadableItemAtPath:(NSString *)path; + (BOOL)isWritableItemAtPath:(NSString *)path; */ // 判斷目錄是否存在 NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; BOOL isExist = [HYFileManager isExistsAtPath:path];
獲取文件(夾)大小
/* All shortcuts suppported: + (NSNumber *)sizeOfItemAtPath:(NSString *)path; + (NSNumber *)sizeOfItemAtPath:(NSString *)path error:(NSError **)error; + (NSNumber *)sizeOfFileAtPath:(NSString *)path; + (NSNumber *)sizeOfFileAtPath:(NSString *)path error:(NSError **)error; + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path; + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path error:(NSError **)error; + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path; + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path error:(NSError **)error; + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path; + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path error:(NSError **)error; + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path; + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path error:(NSError **)error; */ // 獲取文件大小 NSError *error; NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; NSNumber *size = [HYFileManager sizeOfFileAtPath:path error:&error];
寫入文件內容
/* All shortcuts suppported: + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content; + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; */ // 寫入文件內容 NSError *error; NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; BOOL isSuccess = [HYFileManager writeFileAtPath:path content:@"Hello World" error:error];
HYFileManager仍然有很多地方需要改進,如果您喜歡HYFileManager,歡迎star它,如果您有什麼好的建議,可以提issue給我,我會第一時間跟進。
HYFileManager地址:https://github.com/castial/HYFileManager