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
: