[objc] view plain copy print ? -(NSString*)documentsDirectory{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths firstObject]; return documentsDirectory; }
其中,NSDocumentDirectory 是指程序中对应的Documents路径,而NSDocumentionDirectory对应于程序中的Library/Documentation路径,这个路径是没有读写权限的,所以看不到文件生成。
iOS中NSSearchPathForDirectoriesInDomains函数
iOS开发是在沙盒中开发的,对一些部分的文件的读写进行了限制, 只能在几个目录下读写文件: (1)Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录 (2) tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除 (3) Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除 对于文件操作, NSSearchPathForDirectoriesInDomains是核心函数。下面简要分析其试用方法: <p style="margin-top: 8px; margin-bottom: 8px; padding-top: 0px; padding-bottom: 0px; line-height: 23px; font-size: 13px; font-family: Verdana, sans-serif, 宋体; white-space: normal;"><span style="line-height: 22px;"><span class="typ" style="color: rgb(102, 0, 102);">NSArray</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="pln" style="color: rgb(0, 0, 0);">paths</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="typ" style="color: rgb(102, 0, 102);">NSSearchPathForDirectoriesInDomains</span><span class="pun" style="color: rgb(102, 102, 0);">(</span></span><span style="color: rgb(255, 0, 0); line-height: 22px;"><span class="typ" style="color: rgb(102, 0, 102);">NSDocumentDirectory</span></span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span style="line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">NSUserDomainMask</span><span class="pln" style="color: rgb(0, 0, 0);"> </span></span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span style="line-height: 22px;"><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln" style="color: rgb(0, 0, 0);"> YES</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);"> </span></span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span style="line-height: 22px;"><span class="typ" style="color: rgb(102, 0, 102);">NSLog</span><span class="pun" style="color: rgb(102, 102, 0);">(@</span><span class="str" style="color: rgb(0, 136, 0);">"Get document path: %@"</span><span class="pun" style="color: rgb(102, 102, 0);">,[</span><span class="pln" style="color: rgb(0, 0, 0);">paths objectAtIndex</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">]);</span></span></p><p style="margin-top: 8px; margin-bottom: 8px; padding-top: 0px; padding-bottom: 0px; line-height: 23px; font-size: 13px; font-family: Verdana, sans-serif, 宋体; white-space: normal;"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">fileName</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=[[</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">paths objectAtIndex</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="lit" style="line-height: 22px; color: rgb(0, 102, 102);">0</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">]</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> stringByAppendingPathComponent</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">"myFile"</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">];</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">content</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">"write data into myFile"</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">;</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSData</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">contentData</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=[</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">content dataUsingEncoding</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSASCIIStringEncoding</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">];</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span class="kwd" style="line-height: 22px; color: rgb(0, 0, 136);">if</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">([</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">contentData writeToFile</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">fileName atomically</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">YES</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">])</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">{</span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSLog</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">(@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">">>write ok."</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">);</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><br style="line-height: 22px; padding: 0px; margin: 0px;" /><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">}</span></p><p style="margin-top: 8px; margin-bottom: 8px; padding-top: 0px; padding-bottom: 0px; line-height: 23px; font-size: 13px; font-family: Verdana, sans-serif, 宋体; white-space: normal;"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">contentChinese</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">"写入汉字信息到文件"</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">;</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span></p><p style="margin-top: 8px; margin-bottom: 8px; padding-top: 0px; padding-bottom: 0px; line-height: 23px; font-size: 13px; font-family: Verdana, sans-serif, 宋体; white-space: normal;"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSData</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">contentChineseData</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=[</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">contentChinese dataUsingEncoding</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSUnicodeStringEncoding</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">];</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span></p><p style="margin-top: 8px; margin-bottom: 8px; padding-top: 0px; padding-bottom: 0px; line-height: 23px; font-size: 13px; font-family: Verdana, sans-serif, 宋体; white-space: normal;"><span style="line-height: 22px; background-color: rgb(255, 255, 255);"><span style="line-height: 23px;"><span class="kwd" style="line-height: 22px; color: rgb(0, 0, 136);">if</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">([</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">contentData writeToFile</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">fileName atomically</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">YES</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">])</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">{</span></span><br style="line-height: 23px; padding: 0px; margin: 0px;" /><span style="line-height: 23px;"><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSLog</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">(@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">">>写入成功。"</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">);</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span></span></span></p> 如果要指定其他文件目录,比如Caches目录,需要更换目录常量为 NSCachesDirectory。 使用NSSearchPathForDirectoriesInDomains只能定位Caches目录和Documents目录。 tmp目录,不能按照上面的做法获得目录了,有个函数可以获得应用的根目录: <p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal; background-color: rgb(255, 255, 255);"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSHomeDirectory</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">()</span></span></p> 也就是Documents的上级目录,当然也是tmp目录的上级目录。那么文件路径可以这样写: <p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal; background-color: rgb(255, 255, 255);"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">fileName</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=[</span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSHomeDirectory</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">()</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> stringByAppendingPathComponent</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">"tmp/myFile.txt"</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">];</span></span></p> 或者,更直接一点,可以用这个函数: <p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal; background-color: rgb(255, 255, 255);"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSTemporaryDirectory</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">()</span></span></p> 不过生成的路径将可能是: <p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal; background-color: rgb(255, 255, 255);"><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">…/</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">tmp</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">/-</span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">Tmp</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">-/</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">myFile</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">txt</span></span></p>在编写应用项目的时候,常常会使用资源文件,比如:
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="background-color: rgb(255, 255, 255);"><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">myFilePath </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">[[</span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSBundle</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> mainBundle</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">]</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span></span><br style="line-height: 23px; padding: 0px; margin: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;" /><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;"><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> pathForResource</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">"f"</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span></span><br style="line-height: 23px; padding: 0px; margin: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;" /><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;"><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> ofType</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">"txt"</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">];</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span></span><br style="line-height: 23px; padding: 0px; margin: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;" /><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">*</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">myFileContent</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">=[</span><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSString</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> stringWithContentsOfFile</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">myFilePath encoding</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);">NSUTF8StringEncoding error</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">:</span><span class="kwd" style="line-height: 22px; color: rgb(0, 0, 136);">nil</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">];</span><span class="pln" style="line-height: 22px; color: rgb(0, 0, 0);"> </span></span><br style="line-height: 23px; padding: 0px; margin: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;" /><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;"><span class="typ" style="line-height: 22px; color: rgb(102, 0, 102);">NSLog</span><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">(@</span><span class="str" style="line-height: 22px; color: rgb(0, 136, 0);">"bundel file path: %@ \nfile content:</span></span><a target=_blank target="_blank" rel="nofollow" href="mailto:%@",myFilePath,myFileContent" style="text-decoration: none; color: rgb(255, 131, 115); line-height: 23px; padding: 0px; margin: 0px; outline: 0px; font-size: 12px; font-family: Verdana, sans-serif, 宋体; white-space: normal;"><span class="str" style="line-height: 21px; color: rgb(0, 136, 0);">%@"</span><span class="pun" style="line-height: 21px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 21px; color: rgb(0, 0, 0);">myFilePath</span><span class="pun" style="line-height: 21px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 21px; color: rgb(0, 0, 0);">myFileContent</span></a><span style="line-height: 23px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; white-space: normal;"><span class="pun" style="line-height: 22px; color: rgb(102, 102, 0);">);</span></span></span></p>