iOS file Operations (Nsfilemanager)

Source: Internet
Author: User

iOS sandbox mechanism, apps can only access files in their own app directory, iOS is not like Android, no SD card concept, no direct access to images, videos and other content.

Content generated by iOS apps, like, files, cached content, and so on, must be stored in their own sandbox.

By default, each sandbox contains 3 folders:Documents, Library , and tmp. The library contains caches, preferences directories.

Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be kept in that directory, whichis included in itunes Backup and restore.

Library: The default settings or other status information of the stored program;

Library/Caches: Store cache files, save the persisted data of the application, use to apply the upgrade or save the data after the app is closed, not synced by itunes, so in order to reduce the time of synchronization, Consider placing files that are relatively large and that do not need to be backed up in this directory.

tmp: Provides an immediate place to create temporary files, but does not need to be persisted, after the application is closed, the data in the directory will be deleted, or the system may be cleared when the program is not running.

iOS gets the sandbox path, manipulating the file.

Get the app sandbox root path:

-(void) dirhome{nsstring
NSLog (@ "app_home:"%@ ", Dirhome);}

Get documentsdirectory path:
-(NSString *) dirdoc{      //[Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"] ;      Nsarray *paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES);       *documentsdirectory = [Paths Objectatindex:0];      NSLog (@ "app_home_doc:%@", documentsdirectory);       return documentsdirectory;  }  
To get the Library directory path:
-(void) dirlib{      //[Nshomedirectory () stringbyappendingpathcomponent:@ "Library "];      Nsarray *paths = nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES);       *librarydirectory = [Paths Objectatindex:0];      NSLog (@ "app_home_lib:%@", librarydirectory);  }  
get the Cache directory path:
-(void) dircache{      *cacpath = nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES);       *cachepath = [Cacpath objectatindex:0];      NSLog (@ "app_home_lib_cache:%@", CachePath);  }  
get the TMP directory path:
-(void) dirtmp{      //[Nshomedirectory () stringbyappendingpathcomponent:@ "tmp"];      nsstring *tmpdirectory = nstemporarydirectory ();      NSLog (@ "app_home_tmp:%@", tmpdirectory);  }  
To create a folder:
-(void*) createdir{NSString*documentspath =[self dirdoc]; Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; NSString*testdirectory = [Documentspath stringbyappendingpathcomponent:@"Test"]; //Create a directoryBOOL res=[FileManager createdirectoryatpath:testdirectory withintermediatedirectories:yes Attributes:nil Error:nil]; if(res) {NSLog (@"Folder creation succeeded"); }ElseNSLog (@"Folder creation failed"); }  
To create a file:
-(void*) createfile{NSString*documentspath =[self dirdoc]; NSString*testdirectory = [Documentspath stringbyappendingpathcomponent:@"Test"]; Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; NSString*testpath = [Testdirectory stringbyappendingpathcomponent:@"Test.txt"]; BOOL Res=[FileManager Createfileatpath:testpath contents:nil Attributes:nil]; if(res) {NSLog (@"file creation succeeded:%@", Testpath); }ElseNSLog (@"File creation failed"); }  
Write data to file:
-(void) writefile{NSString*documentspath =[self dirdoc]; NSString*testdirectory = [Documentspath stringbyappendingpathcomponent:@"Test"]; NSString*testpath = [Testdirectory stringbyappendingpathcomponent:@"Test.txt"]; NSString*content=@"Test Write Content! "; BOOL Res=[content Writetofile:testpath atomically:yes encoding:nsutf8stringencoding Error:nil]; if(res) {NSLog (@"File Write succeeded"); }ElseNSLog (@"File Write Failed"); }  
Read file data:
-(void) readfile{NSString*documentspath =[self dirdoc]; NSString*testdirectory = [Documentspath stringbyappendingpathcomponent:@"Test"]; NSString*testpath = [Testdirectory stringbyappendingpathcomponent:@"Test.txt"]; //NSData *data = [NSData Datawithcontentsoffile:testpath]; //NSLog (@ "file read succeeded:%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]); NSString *content=[NSString Stringwithcontentsoffile:testpath encoding:nsutf8stringencoding Error:nil]; NSLog (@"file Read success:%@", content); }  
File properties:
-(void) fileattriutes{NSString*documentspath =[self dirdoc]; NSString*testdirectory = [Documentspath stringbyappendingpathcomponent:@"Test"]; Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; NSString*testpath = [Testdirectory stringbyappendingpathcomponent:@"Test.txt"]; Nsdictionary*fileattributes =[FileManager Attributesofitematpath:testpath Error:nil]; Nsarray*keys; IDkey, value; Keys=[FileAttributes AllKeys]; intCount =[keys Count];  for(inti =0; I < count; i++) {Key=[Keys objectatindex:i]; Value=[FileAttributes Objectforkey:key]; NSLog (@"Key:%@ for value:%@", key, value); }  }
To delete a file:
-(void) deletefile{NSString*documentspath =[self dirdoc]; NSString*testdirectory = [Documentspath stringbyappendingpathcomponent:@"Test"]; Nsfilemanager*filemanager =[Nsfilemanager Defaultmanager]; NSString*testpath = [Testdirectory stringbyappendingpathcomponent:@"Test.txt"]; BOOL Res=[FileManager Removeitematpath:testpath Error:nil]; if(res) {NSLog (@"file deletion succeeded"); }ElseNSLog (@"File deletion failed"); NSLog (@"whether the file exists:%@", [FileManager Isexecutablefileatpath:testpath]?@"YES":@"NO"); }  

iOS file Operations (Nsfilemanager)

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.