Detailed IOS Learning notes (17)--File Manipulation (Nsfilemanager) _ios

Source: Internet
Author: User

iOS sandbox mechanism, applications can only access files in their own application directory. Unlike Android, iOS does not have the SD card concept to directly access images, video, and more. The content produced by iOS applications, such as images, files, cached content, etc., must be stored in its 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 pool->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 application browse be saved in the directory, which will be included in the itunes backup and restore.

Library: Stores the default settings or other state information of the program;

Library/caches: Store cached files, save the application of persistent data, for application upgrades or after the application of closed data preservation, will not be synchronized with itunes, so in order to reduce synchronization time, you can consider some of the larger files and do not need to back up the files 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 that directory is deleted, or the system is cleared when the program is not running.

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

Get the application sandbox root path:

-(void) dirhome{ 
  nsstring *dirhome=nshomedirectory ();   
  NSLog (@ "App_home:%@", dirhome); 

Get the Documents directory path:

Access to documents directory 
-(NSString *) dirdoc{ 
  //[nshomedirectory () stringbyappendingpathcomponent:@ "Documents"]; 
  Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); 
  NSString *documentsdirectory = [Paths objectatindex:0]; 
  NSLog (@ "App_home_doc:%@", documentsdirectory); 
  return documentsdirectory; 

Get Library directory path:

Get Library directory 
-(void) dirlib{ 
  //[nshomedirectory () stringbyappendingpathcomponent:@ "Library"]; 
  Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); 
  NSString *librarydirectory = [Paths objectatindex:0]; 
  NSLog (@ "App_home_lib:%@", librarydirectory); 

Get Cache directory path:

Gets the cache directory 
-(void) dircache{ 
  nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); 
  NSString *cachepath = [Cacpath objectatindex:0]; 
  NSLog (@ "App_home_lib_cache:%@", CachePath); 

Get the TMP directory path:

Gets the TMP directory 
-(void) dirtmp{ 
  //[nshomedirectory () stringbyappendingpathcomponent:@ "tmp"]; 
  NSString *tmpdirectory = Nstemporarydirectory (); 
  NSLog (@ "app_home_tmp:%@", tmpdirectory); 

To create a folder:

Create Folder 
-(void *) createdir{ 
  nsstring *documentspath =[self Dirdoc]; 
  Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; 
  NSString *testdirectory = [Documentspath stringbyappendingpathcomponent:@ "test"]; 
  Create a directory 
  BOOL res=[filemanager createdirectoryatpath:testdirectory withintermediatedirectories:yes attributes: Nil Error:nil]; 
  if (res) { 
    NSLog (@ folder creation succeeded); 
  } else 
    NSLog (@ "folder creation failed"); 
 

Create a file

 Create 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"); 
} 

Write data to file:

Write 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"); 
  } else 
    NSLog (@ "file write Failed"); 
} 

Read file data:

Read File 
-(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 successfully:%@", [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]); 
  NSString *content=[nsstring Stringwithcontentsoffile:testpath encoding:nsutf8stringencoding Error:nil]; 
  NSLog (@ "file read succeeded:%@", content); 
} 

File properties:

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; 
  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); 
  } 
 

To delete a file:

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]?@ "YES": @ "NO"); 
} 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.