iOS Nsfilemanager usage detailed

Source: Internet
Author: User
<span id="Label3"></p><p><p>Transferred from: http://blog.csdn.net/ios_che/article/details/7287266</p></p><p><p><strong>iphone File system Nsfilemanager</strong> explanation is the article to introduce the content, mainly through the <strong>iphone file system</strong> to learn how to use the <strong>Nsfilemanager</strong> , the specific content of this article is Detailed.</p></p><p><p>iphone file system: create, rename, and delete files, Nsfilemanager contains methods for querying the word library directory, creating, renaming, deleting directories, and getting/setting file properties (readability, authoring, and so on).</p></p><p><p>Each program will have its own sandbox, through which you can read/write Files. Files written to the sandbox will remain stable in the process of the program, even if the actual program is Updated.</p></p><p><p>As shown below, you can locate the file directory in the Sandbox:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">for error Messages</span></span>Nserror *<span style="color: #000000;"><span style="color: #000000;">error; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">creating a File Manager</span></span>Nsfilemanager *filemgr =<span style="color: #000000;"><span style="color: #000000;">[nsfilemanagerdefaultmanager]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">point to file directory</span></span>NSString *documentsdirectory=<span style="color: #000000;"><span style="color: #000000;">[nshomedirectory () stringbyappendingpathcomponent:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Documents</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create a directory</span></span>[[nsfilemanager defaultmanager] createdirectoryatpath: [nsstring stringwithformat:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@/myfolder</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, Nshomedirectory ()] attributes:nil];</pre><p><p></p></p><p><p><strong>Create a file</strong></p></p><p><p>Now that we have the file directory, we can use this path to create a new file in the sandbox and write a piece of code:</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">file <span style="color: #008000;">we want to creating in the documents directory we want to create will appear in the file directories</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">result Is:/documents/file1.txt result:/documents/file1.txt</span></span>NSString *filepath=<span style="color: #000000;"><span style="color: #000000;">[documentsdirectory stringbyappendingpathcomponent:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">file1.txt</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the string that needs to be written</span></span>NSString *str=<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Iphonedeveloper tips\nhttp://iphonedeveloptips,com</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Write File</span></span><span style="color: #000000;"><span style="color: #000000;">[str writetofile:filepath atomically:yes encoding:nsutf8stringencoding error:</span></span>&<span style="color: #000000;"><span style="color: #000000;">error]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">displaying the contents of a file directory</span></span>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">documentsdirectory:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [filemgr contentsofdirectoryatpath:documentsdirectoryerror:</span></span>&error]);</pre><p><p></p></p><p><p>We build a path (file1.txt) for the file we want to create, initialize a string to write to the file, and list the Directories. The last line shows a list of directories that appear in the file directory after we create the File:</p></p><p><p><strong>Renaming a file</strong></p></p><p><p>To rename a file, we need to move the file to a new path. The following code creates the path to the target file that we expect, and then requests that the file be moved and the file directory displayed after the Move.</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">Rename a <span style="color: #008000;">file by moving the file</span></span>NSString *filepath2=<span style="color: #000000;"><span style="color: #000000;">[documentsdirectory stringbyappendingpathcomponent:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">File2.txt</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine whether to move</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([filemgr Moveitematpath:filepath Topath:filepath2 error:&error]! =<span style="color: #000000;"><span style="color: #000000;">YES) NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Unable to move file:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [error localizeddescription]); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">displaying the contents of a file directory</span></span>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">documentsdirectory:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [filemgr contentsofdirectoryatpath:documentsdirectoryerror:</span></span>&error]);</pre><p><p></p></p><p><p>After the file has been moved, the output should look like the following:</p></p><p><p><strong>Delete a file</strong></p></p><p><p>To make this technique complete, let's look at how to delete a file:</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">Determine whether to delete this file in FilePath2</span> <span style="color: #0000ff;">if</span> ([filemgr removeitematpath:filepath2 error:&error]! =<span style="color: #000000;"> YES) NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">Unable to delete file:%@</span><span style="color: #800000;">"</span><span style="color: #000000;">, [error localizeddescription]); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">displaying the contents of a file directory</span> NSLog (<span style="color: #800000;">@ "</span><span style="color: #800000;">documentsdirectory:%@</span><span style="color: #800000;">"</span><span style="color: #000000;">, [filemgr contentsofdirectoryatpath: Documentsdirectoryerror:</span></pre></pre><p><p></p></p><p><p>Once the file has been deleted, as you might expect, the file directory will be automatically emptied:</p></p><p><p>These examples can teach you only some of the fur on file processing. To get a more comprehensive and detailed explanation, you need to master the knowledge of Nsfilemanager Files.</p></p><p><p>When developing an iphone program, sometimes you need to do something about it. Getting a list of all the files in a directory is one of the basic operations. With this code, you can get a list of files and folders within a Directory.</p></p><pre><pre>Nsfilemanager *filemanager =<span style="color: #000000;"> [nsfilemanager defaultmanager]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">get the list</span> of files and folders in the Application Documents folder here Nsarray *documentpaths =<span style="color: #000000;"> nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES); </span> *documentdir = [documentpaths objectatindex:<span style="color: #800080;">0</span><span style="color: #000000;">]; </span> *error =<span style="color: #000000;"> nil; </span> *filelist =<span style="color: #000000;"> [[nsarray alloc] init]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">filelist is an array of file names and folder names that contain all the files under that folder</span> </pre></pre><p><p></p></p><p><p>The following code can list all the subfolder names in a given folder</p></p><pre>Nsmutablearray *dirarray =<span style="color: #000000;"><span style="color: #000000;">[[nsmutablearray alloc] init]; BOOL Isdir</span></span>=<span style="color: #000000;"><span style="color: #000000;">NO; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">folder name is listed in the FileList obtained in the above program</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(nsstring *file<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">FileList) {nsstring</span></span>*path =<span style="color: #000000;"><span style="color: #000000;">[documentdir stringbyappendingpathcomponent:file]; [filemanager Fileexistsatpath:path isdirectory: (</span></span>&<span style="color: #000000;"><span style="color: #000000;">isdir)]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isdir) {[dirarray addobject:file]; } isdir</span></span>=<span style="color: #000000;"><span style="color: #000000;">NO; } NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">every Thing</span> in the dir:%@</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, fileList); NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;">all <span style="color: #800000;">folders:%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, dirarray);</pre><p><p>Summary: about the <strong>iphone file system Nsfilemanager</strong> explained the content of the introduction, hope that through this study can help you!</p></p><p><p>iOS Nsfilemanager usage detailed</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.