下面是一些简单的关于OC中对文件的基本操作的案例:
//应用程序文件的移动
[[NSFileManager new] moveItemAtPath:(NSString *)/*路径格式(/user/……)-NSString类型*/ toPath:(NSString *) error:(NSError *__autoreleasing *)];
//应用程序文件的复制
[[NSFileManager new] copyItemAtPath:(NSString *) toPath:(NSString *) error:(NSError *__autoreleasing *)];
//应用程序文件的删除
[[NSFileManager new] removeItemAtPath:(NSString *) error:(NSError *__autoreleasing *)];
//应用程序创建文件夹
[[NSFileManager new] createDirectoryAtPath:(NSString *) withIntermediateDirectories:(BOOL) attributes:(NSDictionary *) error:(NSError *__autoreleasing *)];
//应用程序创建文件
[[NSFileManager new] createFileAtPath:(NSString *) contents:(NSData *) attributes:(NSDictionary *)];
一些必要的说明
#define fm [NSFileManager new]
#define DocumentsPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
//宏定义减少代码书写量
typedef enum {
SuccessWriter,
failWriter,
failDownload,
AlreadyDownload,
DownloadAddressError
} DownloadType;
//枚举能够返回各种情况,比BOOL强大。
typedef enum {
Success,
Fail,
NotFind
} removeType;
应用程序文件的写入
(NSString *)urlString CallFileName:(NSString *)fileName {
DownloadType type = failDownload;
if (urlString ==nil) {
return DownloadAddressError;
}
//为了保证健壮性,进行转码,防止中文和特殊符号
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:4];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
if (data == nil) {
return failDownload;
}
if (fileName ==nil) {
fileName = [urlString lastPathComponent];
}
//路径
NSString *filePath =[DocumentsPath stringByAppendingPathComponent:fileName];
//开始写入
if (![fm fileExistsAtPath:filePath]) {
[fm createFileAtPath:filePath contents:data attributes:nil];
type = SuccessWriter;
}else {
type = AlreadyDownload;
}
//判断写入是否成功
if (![fm fileExistsAtPath:filePath]) {
type = failWriter;
}
return type;
}
应用程序文件夹内容的删除
+(removeType) removeAllImageInDirectoryName:(NSString *)directoryName {
//文件夹路径
directoryName = [DocumentsPath stringByAppendingPathComponent:directoryName];
//判断要删除的对象是否存在
if (![fm fileExistsAtPath:directoryName]) {
return NotFind;
}
//找到文件夹中的文件,返回数组
NSArray *array = [fm subpathsAtPath:directoryName];
//遍历数组时删除文件夹中文件
for (NSString *obj in array) {
[fm removeItemAtPath:[directoryName stringByAppendingPathComponent:obj] error:nil];
}
//判断是否删除成功
if ([fm subpathsAtPath:directoryName].count!=0) {
return Fail;
}
return Success;
}
删除指定文件
+(removeType) removeFileName:(NSString *)fileName inDirectoryName:(NSString *)directoryName {
directoryName = [DocumentsPath stringByAppendingPathComponent:directoryName];
fileName = [directoryName stringByAppendingPathComponent:fileName];
if ([fm fileExistsAtPath:fileName]) {
[fm removeItemAtPath:fileName error:nil];
}else {
return NotFind;
}
if ([fm fileExistsAtPath:fileName]) {
return Fail;
}
return Success;
}
求应用文件的大小
+(NSString *) caculateSizeWithDirectoryName:(NSString *)directoryName {
NSString *size = [NSString stringWithFormat:@"error!%@文件不存在!",directoryName];
directoryName = [DocumentsPath stringByAppendingPathComponent:directoryName];
double count = 0.0;
NSArray *array = [fm subpathsAtPath:directoryName];
for (NSString *obj in array) {
NSString *filePath = [directoryName stringByAppendingPathComponent:obj];
NSData *data = [NSData dataWithContentsOfFile:filePath];
count = count + data.length;
}
size = [NSString stringWithFormat:@"%.4lfM",count/1024/1024];
return size;
}