IOS Nsfilemanager file Operations

Source: Internet
Author: User

Reference: http://blog.csdn.net/xyz_lmn/article/details/8968213 thanks to these pioneers

File operations common operations record:

 Gets the root path of the sandbox nsstring *dirhome = Nshomedirectory ();    NSLog (@ "%@", dirhome); Get the Documents directory path Nsarray *path = Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory,nsuserdomainmask,    YES);    NSString *documentsdirectory = [path objectatindex:0];    NSLog (@ "%@", documentsdirectory);    Get the Library directory path = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);    NSString *librarydirectory = [path objectatindex:0];    NSLog (@ "%@", librarydirectory);    Get caches directory path = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);    NSString *caches = [path objectatindex:0];    NSLog (@ "%@", caches);    Get the TMP directory path NSString *tmpdirectory = Nstemporarydirectory ();    NSLog (@ "%@", tmpdirectory); Create folder//1 get file path//2 Create File Manager//3 create file in directory with File Manager Path = Nssearchpathfordirectoriesindomains (Nsdocumentationdi    Rectory,nsuserdomainmask, YES);    documentsdirectory = [path objectatindex:0]; Documentsdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "test"];    Nsfilemanager *FM = [Nsfilemanager Defaultmanager];    BOOL RES = [FM createdirectoryatpath:documentsdirectory withintermediatedirectories:yes Attributes:nil Error:nil];    if (!res) {NSLog (@ "folder creation succeeded");    } else {NSLog (@ "folder creation failed");    }//Create file (same as folder creation) path = Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, Nsuserdomainmask, YES);    documentsdirectory = [path objectatindex:0];    Documentsdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "text"];    Documentsdirectory = [Documentsdirectory stringbyappendingpathcomponent:@ "123.txt"];    FM = [Nsfilemanager Defaultmanager];    RES = [FM createdirectoryatpath:documentsdirectory withintermediatedirectories:yes Attributes:nil Error:nil];    if (res) {NSLog (@ "folder creation succeeded");    } else {NSLog (@ "folder creation failed"); }//write data to file path = Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, NsuseRdomainmask, YES);    documentsdirectory = [path objectatindex:0];    Documentsdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "text"];    Documentsdirectory = [Documentsdirectory stringbyappendingpathcomponent:@ "123.txt"];    NSString *content = @ "file write content";    res = [content writetofile:documentsdirectory atomically:yes encoding:nsutf8stringencoding Error:nil];    if (res) {NSLog (@ "folder creation succeeded");    } else {NSLog (@ "folder creation failed");    }//Read file Data Path = Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, Nsuserdomainmask, YES);    documentsdirectory = [path objectatindex:0];    Documentsdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "text"];    Documentsdirectory = [Documentsdirectory stringbyappendingpathcomponent:@ "123.txt"];    Content = [NSString stringwithcontentsoffile:documentsdirectory encoding:nsutf8stringencoding Error:nil];    NSLog (@ "file read succeeded:%@", content); File's Properties Path = Nssearchpathfordirectoriesindomains (NSdocumentationdirectory, Nsuserdomainmask, YES);    documentsdirectory = [path objectatindex:0];    Documentsdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "text"];    Documentsdirectory = [Documentsdirectory stringbyappendingpathcomponent:@ "123.txt"];    FM = [Nsfilemanager Defaultmanager];    Nsdictionary *fileattributes = [FM attributesofitematpath:documentsdirectory Error:nil];    Nsarray *keys;    ID key,value;    Keys = [FileAttributes AllKeys];    int count = [keys count];        for (int i= 0; i<count; i++) {key = [keys objectatindex:i];        Value = [FileAttributes Objectforkey:key];    NSLog (@ "key:%@ for value:%@", key,value);    }//Delete file path = Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, Nsuserdomainmask, YES);    documentsdirectory = [path objectatindex:0];    Documentsdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "text"]; Documentsdirectory = [Documentsdirectory stringbyappendingpathcomponent:@ "123.txt "];    FM = [Nsfilemanager Defaultmanager];    RES=[FM Removeitematpath:documentsdirectory Error:nil];    if (res) {NSLog (@ "file deletion succeeded");    } {NSLog (@ "File deletion failed"); }





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.