此篇是关于NSFileManager如何使用.包括创建文件,目录.删除文件等操作
获取Documents路径
<pre>- (NSString *)getDocumentsPath { //获取Documents路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSLog(@"path:%@", path); return path; }
</pre>
在Documents里创建目录
<pre>NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSLog(@"%@",filePath); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *fooDirectory = [filePath stringByAppendingPathComponent:@"foo"]; [fileManager createDirectoryAtPath:fooDirectory withIntermediateDirectories:YES attributes:nil error:nil];
</pre>
在foo目录中创建文件,并写入内容
<pre>`
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSLog(@"%@",filePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fooDirectory = [filePath stringByAppendingPathComponent:@"fuck"];
[fileManager createDirectoryAtPath:fooDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePathOftest = [fooDirectory stringByAppendingPathComponent:@"haha001.txt"];
NSString *str = @"write something";
[fileManager createFileAtPath:filePathOftest contents:[str dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
`</pre>
获取文件夹里面所有文件名
<pre>NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSLog(@"%@",filePath); NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *fooDirectory = [filePath stringByAppendingPathComponent:@"fuck"]; NSArray *files = [fileManage subpathsAtPath: fooDirectory ];
</pre>
判断文件是否存在
<pre>`
- (BOOL)isSxistAtPath:(NSString *)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:filePath];
return isExist;
}
`</pre>
计算文件大小
<pre>- (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; } }
</pre>
计算文件夹大小
<pre>`
- (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;
}
}
`</pre>
删除文件
<pre>-(void)deleteFile{ NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *iOSPath = [documentsPath stringByAppendingPathComponent:@"foo.txt"]; BOOL isSuccess = [fileManager removeItemAtPath:iOSPath error:nil]; if (isSuccess) { NSLog(@"delete success"); }else{ NSLog(@"delete fail"); } }
</pre>
移动文件
<pre>- (void)moveFileName { NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"foo.txt"]; NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"foof.txt"]; BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil]; if (isSuccess) { NSLog(@"rename success"); }else{ NSLog(@"rename fail"); } }
</pre>
重命名
<pre>- (void)renameFileName { //通过移动该文件对文件重命名 NSString *documentsPath =[self getDocumentsPath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [documentsPath stringByAppendingPathComponent:@"foo.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"); } }
</pre>
网友评论