iOS SDK详解之NSFileManager
来源:互联网 发布:excel下拉菜单选择数据 编辑:程序博客网 时间:2024/05/17 18:46
出处 http://blog.csdn.net/hello_hwc?viewmode=list
前言:NSFileManager提供了一种方便的方式进行文件操作,包括文件和目录的创建,拷贝,剪切,删除等。
本文会详细讲解如何进行这些最基本的操作。
要注意的几点
- 使用defaultManager的时候,实际上获取的是一个单例(同一个对象),是线程安全的,绝大多数时候,使用这个就可以了。本文讲解基础操作的时候,就使用这个。
- 如果在不同线程中使用,而且需要代理函数来监听事件,这时候要使用init来创建每个线程独立的fileManager
定位
说白了,就是获取一些目录。主要就是两个函数
只是定位
<code class="hljs haml has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-constant" style="box-sizing: border-box;">URLsForDirectory</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:inDomains</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
举例
获取library目录(默认存在)
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> * fileManager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * documentPath = [searchResult firstObject]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>,documentPath);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
定位的时候可以创建
<code class="hljs haml has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-constant" style="box-sizing: border-box;">URLForDirectory</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:inDomain</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:appropriateForURL</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:create</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:error</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
获取Application Support(默认不存在)
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> * fileManager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * path = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span> create:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span> error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>,path);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
这里要提到的几个常用参数
- NSLibraryDirectory - Library目录
- NSApplicationSupportDirectory - Library/Application Support目录
- NSDocumentDirectory - Document 目录
- NSUserDomainMask - 用户域
至于,哪个域存储什么东西,参见我之前写的关于沙盒的文章
http://blog.csdn.net/hello_hwc/article/details/44916909
判断文件/目录是否存在
两个函数
第二个函数还有一个额外输出,如果这个文件存在的话,会给出这个文件是不是目录文件
<code class="hljs haml has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">fileExistsAtPath:</span></span>-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">fileExistsAtPath:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">isDirectory:</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> * fileManager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * documentPath = [searchResult firstObject]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * newPath = [documentPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.path</span> stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Demo/Wenchen"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([fileManager fileExistsAtPath:newPath] == <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Path not exist"</span>); } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> isDic; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([fileManager fileExistsAtPath:documentPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.path</span> isDirectory:&isDic] == <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Path not exist"</span>); } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%d"</span>,isDic);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
创建
创建目录
两个函数参数类似,只不过第一个参数的类型不同
<code class="hljs css has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">-<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">createDirectoryAtURL</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:withIntermediateDirectories</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:attributes</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:error</span>:<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">createDirectoryAtPath</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:withIntermediateDirectories</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:attributes</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:error</span>:</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
返回Bool来反映操作是否成功,如果出错,错误信息在error里
第二个参数代表是否自动创建不存在父目录(也就是一次创建多层目录)
第三个参数用来设置访问权限,通常为nil
举例
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> * fileManager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * documentPath = [searchResult firstObject]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * newPath = [documentPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.path</span> stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Demo/Wenchen"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([fileManager fileExistsAtPath:newPath] == <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>) { [fileManager createDirectoryAtPath:newPath withIntermediateDirectories:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span> attributes:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span> error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
然后,打开沙盒,看到了创建成功
创建文件
使用函数
这里的attributes除非想要设定一些读写权限,否则nil
<code class="hljs haml has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">createFileAtPath:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">contents:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">attributes:</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
- 返回Bool来反映操作是否成功,如果出错,错误信息在error里
这个文件后面要用的
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> * fileManager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * documentPath = [searchResult firstObject]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * newPath = [documentPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.path</span> stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Demo/Wenchen"</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([fileManager fileExistsAtPath:newPath] == <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>) { [fileManager createDirectoryAtPath:newPath withIntermediateDirectories:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span> attributes:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span> error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * filePath = [newPath stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file.txt"</span>]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"blog.csdn.net/hello_hwc"</span>]; [fileManager createFileAtPath:filePath contents:data attributes:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
查看沙盒,确认创建成功
注意,使用一些诸如writeToFile的时候,如果文件不存在,是会自动创建的。
拷贝/移动 文件
使用函数
<code class="hljs haml has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">copyItemAtURL:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">toURL:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">error:</span></span>-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">copyItemAtPath:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">toPath:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">error:</span></span>-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">moveItemAtURL:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">toURL:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">error:</span></span>-<span class="ruby" style="box-sizing: border-box;"> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">moveItemAtPath:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">toPath:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">error:</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
- 返回Bool来反映操作是否成功,如果出错,错误信息在error里
举例
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> * fileManager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * libraryPath = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]firstObject]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * documentPath = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]firstObject]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * oldPath = [libraryPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.path</span> stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Demo/Wenchen/file.txt"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * newPath = [documentPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.path</span> stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file.txt"</span>]; [fileManager copyItemAtPath:oldPath toPath:newPath error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
查看沙盒,拷贝成功
删除
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> * fileManager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> * documentPath = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]firstObject]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * newPath = [documentPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.path</span> stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file.txt"</span>]; Bool success = [fileManager removeItemAtPath:newPath error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];</code>
0 0
- iOS SDK详解之NSFileManager
- iOS SDK详解之NSFileManager
- iOS疯狂详解之NSFileManager使用
- ios NSFileManager 用法详解
- ios NSFileManager 用法详解
- ios NSFileManager 用法详解
- iOS 开发之NSFileManager
- IOS 开发 之 NSFileManager
- iOS NSFileManager 文件操作详解
- IOS SDK详解之NSDictionary
- IOS SDK详解之KVC
- IOS SDK详解之KVO
- IOS SDK详解之NSDictionary
- IOS SDK详解之KVC
- iOS SDK详解之UIScreen
- iOS SDK详解之UIToolBar
- iOS SDK详解之Runloop
- IOS SDK详解之NSDictionary
- 使用firefox插件httperrequest,模拟发送及接收Json请求
- 第65题 Container With Most Water
- 学习笔记:window7环境下的nodejs与npm安装。
- JAVA并发API源码解析:原子类
- firefox 书签同步 xmark + 坚果云webDav
- iOS SDK详解之NSFileManager
- Windows Phone 学习 创建和使用数据库
- Shell脚本执行时出现declare: not found的解决方法
- OC的copy与mutableCopy剖析(copy篇)
- hdu 3335 Divisibility(Dancing Links 重复覆盖)
- 第三章第二十七题
- Iaas Paas Saas
- Eclipse 集成maven 提示错误
- X264 related resource