Detailed description of file read/write tutorials in iPhone Development

Source: Internet
Author: User

IPhone DevelopmentMediumFileThe read/write tutorial is the content to be introduced in this article, mainly to learnIphone DevelopmentAboutFileFor more information, see this article. For an app running on the iPhone, it can only access some of its root directories.FileThe so-called sandbox )..

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 the file:

 
 
  1. de style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left:   
  2. 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "> 
  3. NSArray *paths = NSSearchPa 
  4. thForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
  5.  
  6. de> 
  7. de style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px;   
  8. margin-top: 0px; margin-right: 0px; margin-bottom:   
  9. 0px; margin-left: 0px; ">NSString *documentsDirectory = [paths objectAtIndex:0];  
  10. de> 

.

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

 
 
  1. 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, to create a test directory under Documents,

 
 
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2. NSString *documentsDirectory = [paths objectAtIndex:0];  
  3. NSLog(@”%@”,documentsDirectory);  
  4. NSFileManager *fileManage = [NSFileManager defaultManager];  
  5. NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];  
  6. BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil]; 

Obtain all file names in a directory: The above myDirectory) is available.

 
 
  1. NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil]; 

Or

 
 
  1. NSArray *files = [fileManager subpathsAtPath: myDirectory ]; 

Read an object:

 
 
  1. NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath 

Is the name of the file containing the complete path

Or directly use the NSData class method:

 
 
  1. NSData *data = [NSData dataWithContentOfPath:myFilePath]; 

Save an object:

You can use NSFileManager's

 
 
  1. - (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr; 

Or NSData

 
 
  1. - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;  
  2. - (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr; 

Summary: DetailsIPhone DevelopmentMediumFileThe content of the read/write tutorial is complete. I hope this article will help you!

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.