Writing and reading of iOS files

Source: Internet
Author: User

//Get file path   /** 1 * Bundle is a directory that contains all of the resources of the application, which can be obtained by mainbundle the directory resource*/NSString*filepath = [[NSBundle mainbundle] Pathforresource:@"Contactsinfo"Oftype:nil]; NSLog (@"%@", FilePath); //Take the contents of the file out and store it in a string with the contents of it you can do some of the corresponding operations.NSString *string=[NSString Stringwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil]; NSLog (@"%@",string); //Get the sandbox path get this path to find the AskNSString *sandboxpath =nshomedirectory (); NSLog (@"%@", Sandboxpath); /** * There are 3 folders in the sandbox * 1 documents the file data that is established in the program or browsed in the program is stored in this directory * 2 Library stored program default settings or other status information * 3 TMP Storage Pro When file * 4 application Package*/    //Get document Path//Method 1NSString *documentfilepath = [Sandboxpath stringbyappendingstring:@"/document"]; NSLog (@"%@", Documentfilepath); //Method 2NSString *documentfilepath1 = [Sandboxpath stringbyappendingpathcomponent:@"doucment"]; NSLog (@"%@", documentFilePath1); //Method 3NSString *documentfilepath2 = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0]; NSLog (@"%@", documentFilePath2); //There are three ways to get the Document//writes a string to the specified file a second write overwrites the first writeNSString *afilepath = [DocumentFilePath2 stringbyappendingstring:@"a.txt"]; NSString*str =@"Hello World";        [Str writetofile:afilepath atomically:yes encoding:nsutf8stringencoding Error:nil]; //reads a string from the specified fileNSString *STR2 =[NSString Stringwithcontentsoffile:afilepath encoding:nsutf8stringencoding Error:nil]; NSLog (@"%@", STR2); /** * file writes and reads are conditional nsstring Nsarray nsdictionary NSData These types of data can be written in * * Nsarray nsdictionary NSData In and read out the same way*/

For reference only big God don't squirt

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.