原创Blog,转载请注明出处
http://blog.csdn.net/hello_hwc?viewmode=list
前言:NSFileManager提供了一种方便的方式进行文件操作,包括文件和目录的创建,拷贝,剪切,删除等。
本文会详细讲解如何进行这些最基本的操作。
说白了,就是获取一些目录。主要就是两个函数
只是定位
- URLsForDirectory:inDomains:
举例
获取library目录(默认存在)
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask];
NSURL * documentPath = [searchResult firstObject];
NSLog(@"%@",documentPath);
定位的时候可以创建
- URLForDirectory:inDomain:appropriateForURL:create:error:
获取Application Support(默认不存在)
NSFileManager * fileManager = [NSFileManager defaultManager];
NSURL * path = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSLog(@"%@",path);
这里要提到的几个常用参数
两个函数
第二个函数还有一个额外输出,如果这个文件存在的话,会给出这个文件是不是目录文件
- fileExistsAtPath: - fileExistsAtPath:isDirectory:
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask];
NSURL * documentPath = [searchResult firstObject];
NSString * newPath = [documentPath.path stringByAppendingPathComponent:@"Demo/Wenchen"];
if ([fileManager fileExistsAtPath:newPath] == false) {
NSLog(@"Path not exist");
}
BOOL isDic;
if ([fileManager fileExistsAtPath:documentPath.path isDirectory:&isDic] == false) {
NSLog(@"Path not exist");
}
NSLog(@"%d",isDic);
创建目录
两个函数参数类似,只不过第一个参数的类型不同
-createDirectoryAtURL:withIntermediateDirectories:attributes:error:
- createDirectoryAtPath:withIntermediateDirectories:attributes:error:
返回Bool来反映操作是否成功,如果出错,错误信息在error里
第二个参数代表是否自动创建不存在父目录(也就是一次创建多层目录)
第三个参数用来设置访问权限,通常为nil
举例
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask];
NSURL * documentPath = [searchResult firstObject];
NSString * newPath = [documentPath.path stringByAppendingPathComponent:@"Demo/Wenchen"];
if ([fileManager fileExistsAtPath:newPath] == false) {
[fileManager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
}
然后,打开沙盒,看到了创建成功
使用函数
这里的attributes除非想要设定一些读写权限,否则nil
- createFileAtPath:contents:attributes:
这个文件后面要用的
NSFileManager * fileManager = [NSFileManager defaultManager];
NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask];
NSURL * documentPath = [searchResult firstObject];
NSString * newPath = [documentPath.path stringByAppendingPathComponent:@"Demo/Wenchen"];
if ([fileManager fileExistsAtPath:newPath] == false) {
[fileManager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString * filePath = [newPath stringByAppendingPathComponent:@"file.txt"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@"blog.csdn.net/hello_hwc"];
[fileManager createFileAtPath:filePath contents:data attributes:nil];
注意,使用一些诸如writeToFile的时候,如果文件不存在,是会自动创建的。
使用函数
- copyItemAtURL:toURL:error: - copyItemAtPath:toPath:error: - moveItemAtURL:toURL:error: - moveItemAtPath:toPath:error:
举例
NSFileManager * fileManager = [NSFileManager defaultManager];
NSURL * libraryPath = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]firstObject];
NSURL * documentPath = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]firstObject];
NSString * oldPath = [libraryPath.path stringByAppendingPathComponent:@"Demo/Wenchen/file.txt"];
NSString * newPath = [documentPath.path stringByAppendingPathComponent:@"file.txt"];
[fileManager copyItemAtPath:oldPath toPath:newPath error:nil];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSURL * documentPath = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]firstObject];
NSString * newPath = [documentPath.path stringByAppendingPathComponent:@"file.txt"];
Bool success = [fileManager removeItemAtPath:newPath error:nil];
欢迎关注我的iOS SDK详解专栏
http://blog.csdn.net/column/manage.html?alias=huangwenchen-ios-sdk