NSFileManager是個(gè)單例類镜沽,也是一個(gè)文件管理器敏晤。可以通過NSFileManager創(chuàng)建文件夾缅茉、創(chuàng)建文件嘴脾、寫文件、讀文件內(nèi)容等等基本功能蔬墩。
我們?cè)贒ocuments里面進(jìn)行舉例译打,首先是獲取Documents的路徑。
//獲取Documents路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSLog(@"path:%@", path);
創(chuàng)建文件夾
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [documentArray firstObject];
NSLog(@"documentPath = %@",documentPath);
NSString *filePath = [documentPath stringByAppendingPathComponent:@"ios"];
BOOL isSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%d",isSuccess);
創(chuàng)建文件
NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [documentArray firstObject];
NSLog(@"documentPath = %@",documentPath);
NSString *filePath = [documentPath stringByAppendingPathComponent:@"ios.txt"];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
寫文件
NSString *documentsPath =[self getDocumentsPath];
NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
NSString *content = @"我要寫數(shù)據(jù)啦";
BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
讀取文件內(nèi)容
NSString *documentsPath =[self getDocumentsPath];
NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
NSString *content = [NSString stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"read success: %@",content);
判斷文件是否存在
NSString *filePath = [documentPath stringByAppendingPathComponent:@"ios.txt"];
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
計(jì)算文件大小
- (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;
}
}
計(jì)算整個(gè)文件夾中所有文件大小
- (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;
}
}
刪除文件
-(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");
}
}
移動(dòng)文件
- (void)moveFileName
{
NSString *documentsPath =[self getDocumentsPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"ios/qq"];
BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
if (isSuccess) {
NSLog(@"rename success");
}else{
NSLog(@"rename fail");
}
}
重命名
- (void)renameFileName
{
//通過移動(dòng)該文件對(duì)文件重命名
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");
}
}