#pragma mark - 沙盒目錄相關 ///沙盒的主目錄路徑 + (NSString *)homeDir; ///沙盒中Documents的目錄路徑 + (NSString *)documentsDir; ///沙盒中Library的目錄路徑 + (NSString *)libraryDir; ///沙盒中Library / Preferences的目錄路徑 + (NSString *)preferencesDir; ///沙盒中Library / Caches的目錄路徑 + (NSString *)cachesDir; ///沙盒中tmp的目錄路徑 + (NSString *)tmpDir; #pragma mark - 遍歷文件夾 /** 文件遍歷 @param path 目錄的絕對路徑 @param deep 是否深遍歷 1. 淺遍歷:返回當前目錄下的全部文件和文件夾 2. 深遍歷:返回當前目錄下及子目錄下的全部文件和文件夾 @return 遍歷結果數組 */ + (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep; ///遍歷沙盒主目錄 + (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep; ///遍歷Documents目錄 + (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep; ///遍歷Library目錄 + (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep; ///遍歷Caches目錄 + (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep; ///遍歷tmp目錄 + (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep; #pragma mark - 寫入文件內容 ///寫入文件內容 + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content; ///寫入文件內容(錯誤信息error) + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error;
//獲取documents路徑 NSString * documentsDirPath=[TXFileOperation documentsDir]; //文件夾名稱 NSString * folderName=@"test"; //拼接文件夾路徑 NSString * folderPath=[NSString stringWithFormat:@"%@/%@",documentsDirPath,folderName]; //建立文件夾 if ([TXFileOperation createDirectoryAtPath:folderPath]) { NSLog(@"文件夾建立成功"); NSLog(@"文件夾路徑:%@",folderPath); }else{ NSLog(@"文件夾建立失敗"); } //文件名稱 NSString * fileName=@"text.txt"; //拼接文件路徑 NSString * filePath=[NSString stringWithFormat:@"%@/%@",folderPath,fileName]; //建立文件 if ([TXFileOperation createFileAtPath:filePath]) { NSLog(@"文件建立成功"); NSLog(@"文件路徑:%@",filePath); }else{ NSLog(@"文件建立失敗"); } //向文件中寫入內容 if ([TXFileOperation writeFileAtPath:filePath content:self.textView.text]) { NSLog(@"文件內容寫入成功"); NSLog(@"寫入內容:%@",self.textView.text); }else{ NSLog(@"文件內容寫入失敗"); }
查看項目源碼請點這裏!git