iOS之文件管理(二)—NSFileManager
一,前言
NSFileManager:用于执行一般的文件系统操作,主要功能包括:从一个文件中读取数据,向一个文件中写入数据,删除文件,复制文件,移动文件,比较两个文件的内容,测试文件的存在性,读取/更改文件的属性等
访问NSFileManager,使用共享的管理器对象
NSFileManager *fileManager = [NSFileManager defaultManager];
允许对NSFileManager设置代理,用于当文件管理器完成一项操作,如复制或移动文件操作时,接受相应的信息,设置代理时,需要创建自己的NSFileManager实例,而不是使用共享实例
NSFileManager *fileManager = [[NSFileManager alloc] init]; fileManager.delegate = self;
二,相关操作
1. 创建文件夹/目录(返回创建结果)
-(BOOL)createDir:(NSString *)fileName{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString * path = [NSString stringWithFormat:@"%@/%@",documentsDirectory,fileName]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isDir; if (![fileManager fileExistsAtPath:path isDirectory:&isDir]) {
//先判断目录是否存在,不存在才创建 BOOL res=[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; return res; } else {
return NO;
}
2. 创建文件(返回创建结果)
NSString *documentsPath =[self getDocumentsPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
BOOL isSuccess = [fileManager createFileAtPath:iOSPath contents:nil attributes:nil];
if (isSuccess) {
NSLog(@"success");
} else {
NSLog(@"fail");
}
3.写数据到文件(返回写入结果)
- (void)writeFile { NSString *documentsPath =[self getDocumentsPath]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *content = @"我要写数据啦"; BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; if (isSuccess) { NSLog(@"write success"); } else { NSLog(@"write fail"); } }
4.读取文件
- (void)readFileContent { NSString *documentsPath =[self getDocumentsPath]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"read success: %@",content); }
5.保存文件
+(BOOL)saveToDirectory:(NSString *)path data:(NSData *)data name:(NSString *)newName { NSString * resultPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",newName]]; return [[NSFileManager defaultManager] createFileAtPath:resultPath contents:data attributes:nil]; }
6.判断文件是否存在
- (BOOL)isSxistAtPath:(NSString *)filePath { NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isExist = [fileManager fileExistsAtPath:filePath]; return isExist; }
7.计算文件大小
- (unsigned long long)fileSizeAtPath:(NSString *)filePath {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:filePath];
if (isExist) {
unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
return fileSize;
} else {
NSLog(@"file is not exist"); return 0;
}
}
8.计算整个文件夹中所有文件大小
- (unsigned long long)folderSizeAtPath:(NSString*)folderPath { NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL isExist = [fileManager fileExistsAtPath:folderPath]; if (isExist) { NSEnumerator *childFileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator]; unsigned long long folderSize = 0; NSString *fileName = @""; while ((fileName = [childFileEnumerator nextObject]) != nil){ NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName]; folderSize += [self fileSizeAtPath:fileAbsolutePath]; } return folderSize / (1024.0 * 1024.0); } else { NSLog(@"file is not exist"); return 0; } }
9.删除文件
- (void)deleteFile { NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; BOOL isSuccess = [fileManager removeItemAtPath:iOSPath error:nil]; if (isSuccess) { NSLog(@"delete success"); }else{ NSLog(@"delete fail"); } }
10.移动文件
- (void)moveFileName { NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil]; if (isSuccess) { NSLog(@"rename success"); }else{ NSLog(@"rename fail"); } }
11.重命名
- (void)renameFileName { //通过移动该文件对文件重命名 NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"]; NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"]; BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil]; if (isSuccess) { NSLog(@"rename success"); }else{ NSLog(@"rename fail"); } }
12,文件属性
-(void)fileAttriutes:(NSString *)path{ NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil]; NSArray *keys; id key, value; keys = [fileAttributes allKeys]; int count = [keys count]; for (int i = 0; i < count; i++) { key = [keys objectAtIndex: i]; //获取文件名 value = [fileAttributes objectForKey: key]; //获取文件属性 } }
13.根据路径复制文件
+(BOOL)copyFile:(NSString *)path topath:(NSString *)topath { BOOL result = NO; NSError * error = nil; result = [[NSFileManager defaultManager]copyItemAtPath:path toPath:topath error:&error ]; if (error){ NSLog(@"copy失败:%@",[error localizedDescription]); } return result; }
14.根据路径剪切文件
+(BOOL)cutFile:(NSString *)path topath:(NSString *)topath { BOOL result = NO; NSError * error = nil; result = [[NSFileManager defaultManager]moveItemAtPath:path toPath:topath error:&error ]; if (error){ NSLog(@"cut失败:%@",[error localizedDescription]); } return result; }
15.根据文件路径获取文件名称
+(NSString *)getFileNameByPath:(NSString *)filepath { NSArray *array=[filepath componentsSeparatedByString:@"/"]; if (array.count==0) { return filepath; } return [array objectAtIndex:array.count-1]; }
16.根据路径获取该路径下所有目录
+(NSArray *)getAllFileByName:(NSString *)path { NSFileManager *defaultManager = [NSFileManager defaultManager]; NSArray *array = [defaultManager contentsOfDirectoryAtPath:path error:nil]; return array; }
17.根据路径获取文件目录下所有文件
+(NSArray *)getAllFloderByName:(NSString *)path { NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray * fileAndFloderArr = [self getAllFileByName:path]; NSMutableArray *dirArray = [[NSMutableArray alloc] init]; BOOL isDir = NO; //在上面那段程序中获得的fileList中列出文件夹名 for (NSString * file in fileAndFloderArr){ NSString *paths = [path stringByAppendingPathComponent:file]; [fileManager fileExistsAtPath:paths isDirectory:(&isDir)]; if (isDir) { [dirArray addObject:file]; } isDir = NO; } return dirArray; }
18.获取文件创建时间
+ (NSString *)getFileCreatDateWithPath:(NSString *)path{ NSString *date = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil]; date = [fileAttributes objectForKey:NSFileCreationDate]; return date; }
19.获取文件所有者
+ (NSString *)getFileOwnerWithPath:(NSString *)path { NSString *fileOwner = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil]; fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName]; return fileOwner; }
20.获取文件更改日期
+ (NSString *)getFileChangeDateWithPath:(NSString *)path { NSString *date = nil; NSFileManager *fileManager = [NSFileManager defaultManager]; NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil]; date = [fileAttributes objectForKey:NSFileModificationDate]; return date; }