您的位置:首页 > 移动开发 > IOS开发

iOS SDK详解之NSFileManager

2015-05-07 13:14 253 查看
原创Blog,转载请注明出处

http://blog.csdn.net/hello_hwc?viewmode=list

前言:NSFileManager提供了一种方便的方式进行文件操作,包括文件和目录的创建,拷贝,剪切,删除等。

本文会详细讲解如何进行这些最基本的操作。

要注意的几点

使用defaultManager的时候,实际上获取的是一个单例(同一个对象),是线程安全的,绝大多数时候,使用这个就可以了。本文讲解基础操作的时候,就使用这个。

如果在不同线程中使用,而且需要代理函数来监听事件,这时候要使用init来创建每个线程独立的fileManager

定位

说白了,就是获取一些目录。主要就是两个函数

只是定位

- 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);


这里要提到的几个常用参数

NSLibraryDirectory - Library目录

NSApplicationSupportDirectory - Library/Application Support目录

NSDocumentDirectory - Document 目录

NSUserDomainMask - 用户域

至于,哪个域存储什么东西,参见我之前写的关于沙盒的文章

/article/1371325.html

判断文件/目录是否存在

两个函数

第二个函数还有一个额外输出,如果这个文件存在的话,会给出这个文件是不是目录文件

- 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:


返回Bool来反映操作是否成功,如果出错,错误信息在error里

这个文件后面要用的

  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:


返回Bool来反映操作是否成功,如果出错,错误信息在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: