ios--File Management Nsfilemanager

Source: Internet
Author: User
Tags tmp folder

Sandbox mechanism for iOS. Apps can only access files in their app folders. iOS is not like Android. There is no SD card concept. You cannot directly access 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 includes caches, preferences folders. Documents: Apple recommends that the files created by the program and the file data generated by the app's browsing be saved in this folder, and itunes backup and restore will include this folder library: default settings or other status information for the stored program; library/ Caches: Store the cache file and save the persisted data of the app. For app upgrades or app-closed data save, not synced by itunes, so in order to reduce the time to sync, consider putting some larger files into this folder without needing to back up the files. TMP: Provides a place to instantly create temporary files, but does not need to be persisted. After the app is closed, the data under the folder is deleted. It is also possible that the system is cleared when the program does not execute. A: Get app sandbox root path:-(void) dirhome{nsstring *dirhome=nshomedirectory (); NSLog (@ "App_home:%@", dirhome); } B: Get Documents Folder path:-(NSString *) dirdoc{//[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentsdirectory = [Paths objectatindex:0]; NSLog (@ "App_home_doc:%@", documentsdirectory); return documentsdirectory; } C: Get library folder path:-(void) dirlib{//[nshomedirectory () stringbyappendingpathcomponent:@ "Library"]; Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString *libraryDirectory = [Paths objectatindex:0]; NSLog (@ "App_home_lib:%@", librarydirectory); } D: Get the cache folder path:-(void) dircache{Nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, NSUserDo Mainmask, YES); NSString *cachepath = [Cacpath objectatindex:0]; NSLog (@ "App_home_lib_cache:%@", CachePath); } e: Get tmp folder path:-(void) dirtmp{//[nshomedirectory () stringbyappendingpathcomponent:@ "TMP"]; NSString *tmpdirectory = Nstemporarydirectory (); NSLog (@ "app_home_tmp:%@", tmpdirectory); } F: Create folder:-(void *) createdir{nsstring *documentspath =[self Dirdoc]; Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"]; Create folder BOOL Res=[filemanager createdirectoryatpath:testdirectory withintermediatedirectories:yes attributes:nil Err Or:nil]; if (res) {NSLog (@ "folder creation succeeded"); }else NSLog (@ "folder creation failed"); } g: Creating 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); }else NSLog (@ "File creation failed"); } h: Write data to File:-(void) writefile{nsstring *documentspath =[self Dirdoc]; NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"]; NSString *testpath = [testdirectory stringbyappendingpathcomponent:@ "test.txt"]; NSString *[email protected] "Test write content! "; BOOL res=[content writetofile:testpath atomically:yes encoding:nsutf8stringencoding Error:nil]; if (res) {NSLog (@ "file write succeeded"); }else NSLog (@ "file write Failed"); } I: 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 succeeded:%@", content); } J: File attribute:-(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; ID key, value; Keys = [FileAttributes AllKeys]; int CouNT = [keys Count]; for (int i = 0; i < count; i++) {key = [keys objectatindex:i]; Value = [FileAttributes Objectforkey:key]; NSLog (@ "key:%@ for Value:%@", key, value); }} k: delete 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"); }else NSLog (@ "File deletion failed"); NSLog (@ "file exists:%@", [FileManager isexecutablefileatpath:testpath][email protected] "yes": @ "NO"); }


ios--File Management 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.