iOS 文件管理:FCFileManager

jopen 10年前發布 | 37K 次閱讀 iOS開發 移動開發 FCFileManager

FCFileManager 是一個構建在 NSFileManager 之上的 iOS 文件管理工具,簡化了文件管理。它提供了許多靜態方法,用于執行最常用的操作用幾行代碼。它的工作原理是默認的文件目錄,允許使用相對路徑,但它可以在任何其他目錄中輕松工作。

特性

  • Build paths relative to absolute directories (FCFileManager works by default in the Documents directory, so you must build absolute paths only if you need to work outside of the Documents directory)
  • Copy files/directories
  • Create files/directories
  • Check if files/directory exists
  • Get files/directories attributes (creation date, size, ...)
  • List files/directories
  • Move files/directories
  • Read/Write files content in different formats (arrays, custom models, data, dictionaries, images, json, strings, ... )
  • Remove files/directories
  • Rename files/directories
  • Directories are created on the fly
  • Error handling as using NSFileManager

構建路徑:

//my file path, this will be automatically used as it's relative to the Documents directory
NSString *testPath = @"test.txt";
//my file path relative to the temporary directory path
NSString *testPathTemp = [FCFileManager pathForTemporaryDirectoryWithPath:testPath];

/*
All shortcuts suppported:

pathForApplicationSupportDirectory;
pathForCachesDirectory;
pathForDocumentsDirectory;
pathForMainBundleDirectory;
pathForPlistNamed:(NSString *)name; //look for {{ name }}.plist in the main bundle directory
pathForTemporaryDirectory;
*/

復制文件:

//copy file from Documents directory (public) to ApplicationSupport directory (private)
NSString *testPath = [FCFileManager pathForApplicationSupportDirectoryWithPath:@"test-copy.txt"];
[FCFileManager copyItemAtPath:@"test.txt" toPath:testPath];

創建文件:

//create file and write content to it (if it doesn't exist) 
[FCFileManager createFileAtPath:@"test.txt" withContent:@"File management has never been so easy!!!"]; 

Create directories:

//create directories tree for the given path (in this case in the Documents directory)
[FCFileManager createDirectoriesForPath:@"/a/b/c/d/"];

Check if file exists:

//check if file exist and returns YES or NO
BOOL testFileExists = [FCFileManager existsItemAtPath:@"test.txt"];

Move file:

//move file from a path to another and returns YES or NO
[FCFileManager moveItemAtPath:@"test.txt" toPath:@"tests/test.txt"];

Read file:

//read file from path and returns its content (NSString in this case)
NSString *test = [FCFileManager readFileAtPath:@"test.txt"];

Remove file:

//remove file at the specified path
[FCFileManager removeItemAtPath:@"test.txt"];

Rename file:

//rename file at the specified path with the new name
[FCFileManager renameItemAtPath:@"test.txt" withName:@"test-renamed.txt"];

Write file:

NSArray *testContent = [NSArray arrayWithObjects:@"t", @"e", @"s", @"t", nil];

//write file at the specified path with content
[FCFileManager writeFileAtPath:@"test.txt" content:testContent];

項目主頁:http://www.baiduhome.net/lib/view/home/1414131885356

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!