IOS File Processing (1)

Source: Internet
Author: User

1. store files in documents, TMP, and library

Documents: used to store regular files that are frequently read or written by applications.

TMP: used to store files generated when the application is running. (The value of exploitation is lost when the application is closed)

Library: generally stores application configuration files, such as plist files.

 

Ii. Reading and Writing files

1. Create an empty application and add the homeviewcontroller file.

Homeviewcontroller. H code:

# Import <uikit/uikit. h>

@ Interface homeviewcontroller: uiviewcontroller {}-(nsstring *) documentspath; // obtains the location of the Documents folder-(nsstring *) readfromfile :( nsstring *) filepath; // read the file content-(void) writetofile :( nsstring *) text withfilename :( nsstring *) filepath; // write the content to the specified file @ end

Homeviewcontroller. m code:

# Import "homeviewcontroller. H "@ interface homeviewcontroller () @ end @ implementation homeviewcontroller // obtains the location of the Documents folder-(nsstring *) documentspath {nsarray * paths = paths (nsdocumentdirectory, nsuserdomainmask, yes ); nsstring * documentsdir = [paths objectatindex: 0]; return documentsdir ;}

 

// Read the file content-(nsstring *) readfromfile :( nsstring *) filepath {If ([[nsfilemanager defaultmanager] fileexistsatpath: filepath]) {nsarray * content = [[nsarray alloc] initwithcontentsoffile: filepath]; nsstring * Data = [[nsstring alloc] initwithformat: @ "% @", [content objectatindex: 0]; [content release]; return data;} else {return nil ;}// write the content to the specified file-(void) writetofile :( nsstring *) text withfilename :( nsstring *) filepath {nsmutablearray * array = [[nsmutablearray alloc] init]; [array addobject: text]; [array writetofile: filepath atomically: Yes]; [array release];}

 

-(Nsstring *) temppath {return nstemporarydirectory ();}-(void) viewdidload {nsstring * filename = [[self documentspath] stringbyappendingpathcomponent: @ "content.txt"]; // nsstring * filename = [[self temppath] stringbyappendingpathcomponent: @ "content.txt"]; [self writetofile: @ "Apple charm! "Withfilename: Filename]; nsstring * filecontent = [self readfromfile: Filename]; nslog (filecontent); [Super viewdidload];} @ end

 

:

 

 

 

 

 

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.