iOS Learning Notes (17)--File operations (Nsfilemanager)

Source: Internet
Author: User

http://blog.csdn.net/xyz_lmn/article/details/8968213

The sandbox mechanism for iOS, apps can only access files in their own app directory. Unlike Android, iOS does not have an SD card concept and does not have 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.

The full path above is: User--resource library->application Support->iphone simulator->6.1->aplications

Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be saved in this directory, whichis included in itunes backup and restore
Library: The default settings or other status information of the stored program;

Library/caches: Store the cache file, save the persisted data of the application, use it to apply the upgrade or save the data after the app is closed, and not be synced by itunes, so in order to reduce the time of synchronization, consider putting some larger files into this directory without needing backup.

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.

APP Sandbox

How does iOS get the sandbox path and how do I manipulate files? Here's the answer.

Get the app sandbox root path:

[CPP]View PlainCopyprint?
    1. -(void) dirhome{
    2. NSString *dirhome=nshomedirectory ();
    3. NSLog (@"App_home:%@", dirhome);
    4. }

Get documentsdirectory path:

[CPP]View PlainCopyprint?
    1. //get documents directory   
    2. -(nsstring *) dirdoc{  
    3.     //[ Nshomedirectory ()  stringbyappendingpathcomponent:@ "Documents"];  
    4.      nsarray *paths = nssearchpathfordirectoriesindomains (NSDocumentDirectory,  Nsuserdomainmask, yes);   
    5.     nsstring * documentsdirectory = [paths objectatindex:0];  
    6.     nslog (@
    7.     return documentsdirectory;  
    8. }  

To get the Library directory path:

[CPP]View PlainCopyprint?
    1. Get Library Directory
    2. -(void) dirlib{
    3. //[nshomedirectory () stringbyappendingpathcomponent:@ "Library";
    4. Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);
    5. NSString *librarydirectory = [Paths objectatindex:0];
    6. NSLog (@"App_home_lib:%@", librarydirectory);
    7. }

get the Cache directory path:

[CPP]View PlainCopyprint?
    1. Get the Cache directory
    2. -(void) dircache{
    3. Nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
    4. NSString *cachepath = [Cacpath objectatindex:0];
    5. NSLog (@"App_home_lib_cache:%@", CachePath);
    6. }

get the TMP directory path:

[CPP]View PlainCopyprint?
    1. Get the TMP directory
    2. -(void) dirtmp{
    3. //[nshomedirectory () stringbyappendingpathcomponent:@ "TMP"];
    4. NSString *tmpdirectory = Nstemporarydirectory ();
    5. NSLog (@"app_home_tmp:%@", tmpdirectory);
    6. }

To create a folder:

[CPP]View PlainCopyprint?
  1. Create a folder
  2. -(void *) createdir{
  3. NSString *documentspath =[self Dirdoc];
  4. Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
  5. NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
  6. //Create a directory
  7. BOOL res=[filemanager createdirectoryatpath:testdirectory withintermediatedirectories:yes attributes:nil Error:  NIL];
  8. if (res) {
  9. NSLog (@"folder creation succeeded");
  10. }Else
  11. NSLog (@"folder creation failed");
  12. }



Create a file

[CPP]View PlainCopyprint?
  1. Create a file
  2. -(void *) createfile{
  3. NSString *documentspath =[self Dirdoc];
  4. NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
  5. Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
  6. NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
  7. BOOL res=[filemanager createfileatpath:testpath contents:nil Attributes:nil];
  8. if (res) {
  9. NSLog (@"file creation succeeded:%@", Testpath);
  10. }Else
  11. NSLog (@"file creation failed");
  12. }

Write data to file:

[CPP]View PlainCopyprint?
  1. Write a file
  2. -(void) writefile{
  3. NSString *documentspath =[self Dirdoc];
  4. NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
  5. NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
  6. NSString *[email protected]"Test write content!  ";
  7. BOOL res=[content writetofile:testpath atomically:yes encoding:nsutf8stringencoding Error:nil];
  8. if (res) {
  9. NSLog (@"file write succeeded");
  10. }Else
  11. NSLog (@"file write Failed");
  12. }

Read file data:

[CPP]View PlainCopyprint?
  1. Read the file
  2. -(void) readfile{
  3. NSString *documentspath =[self Dirdoc];
  4. NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
  5. NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
  6. NSData *data = [NSData Datawithcontentsoffile:testpath];
  7. NSLog (@ "file read succeeded:%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]);
  8. NSString *content=[nsstring Stringwithcontentsoffile:testpath encoding:nsutf8stringencoding Error:nil];
  9. NSLog (@"file read succeeded:%@", content);
  10. }

File properties:

[CPP]View PlainCopyprint?
  1. File properties
  2. -(void) fileattriutes{
  3. NSString *documentspath =[self Dirdoc];
  4. NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
  5. Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
  6. NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
  7. Nsdictionary *fileattributes = [FileManager attributesofitematpath:testpath error:nil];
  8. Nsarray *keys;
  9. ID key, value;
  10. Keys = [FileAttributes AllKeys];
  11. int count = [keys count];
  12. For (int i = 0; i < count; i++)
  13. {
  14. key = [Keys objectatindex:i];
  15. Value = [FileAttributes Objectforkey:key];
  16. NSLog (@"key:%@ for Value:%@", key, value);
  17. }
  18. }

To delete a file:

[CPP]View PlainCopyprint?
  1. deleting files
  2. -(void) deletefile{
  3. NSString *documentspath =[self Dirdoc];
  4. NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@"test"];
  5. Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
  6. NSString *testpath = [testdirectory stringbyappendingpathcomponent:@"Test.txt"];
  7. BOOL Res=[filemanager Removeitematpath:testpath Error:nil];
  8. if (res) {
  9. NSLog (@"file deletion succeeded");
  10. }Else
  11. NSLog (@"file deletion failed");
  12. NSLog (@"file exists:%@", [FileManager isexecutablefileatpath:testpath][email protected]"yes": @"NO");
  13. }

/*** @author Zhang Xingye * http://blog.csdn.net/xyz_lmn* iOS starter Group: 83702688* Android Development Advanced Group: 241395671* my Sina Weibo: @ Zhang Xingye tbow* My e-mail: xy-zhang#163.com (#->@) */https://developer.apple.com/library/ios/#documentation/filemanagement/ Conceptual/filesystemprogrammingguide/filesystemoverview/filesystemoverview.html#//apple_ref/doc/uid/ Tp40010672-ch2-sw2

iOS Learning Notes (17)--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.