For an app running on the iPhone, it can only access some files (so-called sandbox) in its root directory ).
After an app is published to the iPhone, its directory structure is as follows:
1. The app root can be accessed using nshomedirectory (). 2. The documents directory is the place where we can write and save files. Generally, we can use:
Nsarray
* Paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory,
Nsuserdomainmask, yes); nsstring * documentsdirectory = [paths
Objectatindex: 0];
.
3. In the tmp directory, we can write data that is needed when the program is running. The data written in the tmp directory will not exist after the program exits. You can use
Nsstring * nstemporarydirectory (void); method;
4. file operations can be performed through nsfilemanage, And the instance can be obtained through [nsfilemanger defaultmanger.
Related Operations:
Create a directory:
For example, if you want to create a test directory under documents,
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Nslog (@ "% @", documentsdirectory); nsfilemanager
* Filemanage = [nsfilemanager defaultmanager]; nsstring * mydirectory =
[Documentsdirectory stringbyappendingpathcomponent: @ "test"]; bool OK =
[Filemanage createdirectoryatpath: mydirectory attributes: Nil];Get all file names in a directory:
(Above mydirectory) Available
Nsarray * file = [filemanager subpathsofdirectoryatpath: mydirectory error: Nil]; or
Nsarray * files = [filemanager subpathsatpath: mydirectory];
Read an object:
Nsdata * Data = [filemanger contentsatpath: myfilepath]; // myfilepath is the file name that contains the complete path
Or directly use the nsdata class method:
Nsdata * Data = [nsdata datawithcontentofpath: myfilepath];
Save an object:
You can use nsfilemanager's
-(Bool) createfileatpath :( nsstring *) path contents :( nsdata *) data attributes :( nsdictionary *) ATTR;
Or nsdata
-(Bool) writetofile :( nsstring *) path atomically :( bool) useauxiliaryfile;
-(Bool) writetofile :( nsstring *) Path Options :( nsuinteger) writeoptionsmask error :( nserror **) errorptr;