FCFileManager 1.0.20

FCFileManager 1.0.20

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2017年12月

Fabio Caccamo 维护。



  • 作者
  • Fabio Caccamo

基于 NSFileManager 的 iOS 文件管理器,简化文件管理。 它提供许多静态方法,可以以很少的代码执行大多数常见操作。默认情况下,它工作在 Documents 目录,允许使用相对路径,但也可以轻松地在任何其他目录中工作。

需求

  • iOS >= 5.0
  • 启用 ARC

安装

手动安装

FCFileManager.hFCFileManager.m 复制到您的项目中。

功能

  • 构建相对于绝对目录的路径 (FCFileManager 默认在 Documents 目录工作,因此仅当您需要在工作在 Documents 目录之外时才需要构建绝对路径)
  • 复制文件/目录
  • 创建文件/目录
  • 检查文件/目录是否存在
  • 获取文件/目录的属性 (创建日期,大小,...)
  • 列出文件/目录
  • 移动文件/目录
  • 以不同格式读取/写入文件内容(数组,自定义模型,数据,字典,图像,json,字符串,...)
  • 读取/写入 xattr (扩展文件属性)
  • 读取图像元数据,EXIF数据,TIFF数据
  • 删除文件/目录
  • 重命名文件/目录
  • 目录会即时创建
  • 错误处理与使用 NSFileManager 类似

查看 FCFileManager.h 了解所有方法。

使用示例

构建路径

//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;
pathForLibraryDirectory;
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 tree for the given path (in this case in the Documents directory)
[FCFileManager createDirectoriesForPath:@"/a/b/c/d/"];

检查文件是否存在

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

移动文件

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

读取文件

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

读取/写入 xattr(扩展文件属性)

//returns the string-value stored for the specified key, if the key doesn't exist returns nil
NSString *value = [FCFileManager xattrOfItemAtPath:@"test.txt" getValueForKey:"uploaded"];

//set the specified string-value and returns a BOOL result of the operation
BOOL success = [FCFileManager xattrOfItemAtPath:@"test.txt" setValue:@"1" forKey:@"uploaded"];

读取图像 EXIF 数据

//read image file from path and returns its EXIF data
NSDictionary *exifData = [FCFileManager exifDataOfImageAtPath:@"test.jpg"];

删除文件

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

重命名文件

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

写入文件

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

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

获取文件/目录大小

//get the file size in bytes
NSNumber *fileSize = [FCFileManager sizeOfFileAtPath:@"test.txt"];

//get the directory size in bytes (including all subdirectories and files inside it)
NSNumber *directorySize = [FCFileManager sizeOfDirectoryAtPath:@"/a/"];

获取文件/目录大小(格式化)

//returns a human-readable file size formatted with the necessary suffix: bytes, KB, MB, GB, TB
NSString *fileSizeFormatted = [FCFileManager sizeFormattedOfFileAtPath:@"test.txt"];

支持开发

Donate

Donate

许可证

根据 MIT 许可证 发布。