Detailed Explanation and Simple Example of IOS File Reading and Writing Operation

  • 2021-12-11 09:13:56
  • OfStack

iPhone file read and write operations

1. Write file operation


- (IBAction)btnWrite:(id)sender {
  // Create a file manager 
  NSFileManager *fileManager = [NSFileManager defaultManager];
  // Get the path 
  // Parameter   Which path to get 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentDirectory = [paths objectAtIndex:0];// Remove the required path 
  // Change to the directory of the operation 
  [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]];
   // Create a file fileName File name, contents File content, if there is no content at first, it can be set to nil , attributes Attributes of a file , Initially nil
  [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];
  // Delete files to be deleted 
  [fileManager removeItemAtPath:@"createNewFile" error:nil];
  // Get the file path 
  NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"];
  NSLog(@"path == %@",path);
  // Data to be written 
  NSString *temp = @"Hello world";
  int data0 = 1000000;
  float data2 = 23.23f;
  // Create a data buffer 
  NSMutableData *writer = [[NSMutableData alloc]init];
  // Add a string to the buffer 
  [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
  [writer appendBytes:&data0 length:sizeof(data0)];
  [writer appendBytes:&data2 length:sizeof(data2)];
   
  [writer writeToFile:path atomically:YES];
  [writer release];  
}

2. Read file operation


- (IBAction)btnRead:(id)sender {
  // Create a file manager 
  NSFileManager *fileManager = [NSFileManager defaultManager];
  // Get the path 
  // Parameter   Which path to get 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentDirectory = [paths objectAtIndex:0];// Remove the required path 
  // Change to the directory of the operation 
  [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]];
   // Create a file fileName File name, contents File content, if there is no content at first, it can be set to nil , attributes Attributes of a file , Initially nil
  [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];
  // Delete files to be deleted 
  [fileManager removeItemAtPath:@"createNewFile" error:nil];
  // Get the file path 
  NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"];
  NSLog(@"path == %@",path);
  // Data to be written 
  NSString *temp = @"Hello world";
  int data0 = 1000000;
  float data2 = 23.23f;
  // Create a data buffer 
  NSMutableData *writer = [[NSMutableData alloc]init];
  // Add a string to the buffer 
  [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
  [writer appendBytes:&data0 length:sizeof(data0)];
  [writer appendBytes:&data2 length:sizeof(data2)];
   
  [writer writeToFile:path atomically:YES];
  [writer release];
  
  // Read operation 
  int gData0;
  float gData1;
  NSString *gData2;
  
  NSData *reader = [NSData dataWithContentsOfFile:path];
  gData2 = [[NSString alloc]initWithData:[reader subdataWithRange:NSMakeRange(0, [temp length])] encoding:NSUTF8StringEncoding];
  [reader getBytes:&gData0 range:NSMakeRange([temp length], sizeof(gData0))];
  [reader getBytes:&gData1 range:NSMakeRange([temp length]+ sizeof(gData0) , sizeof(gData1))];
  NSLog(@"gData0 == %d",gData0);
  NSLog(@"gData1 == %f",gData1);
  NSLog(@"gData2 == %@",gData2);
  
  // lblText.text = gData2;
   
}

iphone Realizes File Read and Write Operation

iphone can easily read and write files, but if you don't use Apple's $99 developer sdk, you can't write files to your mobile phone by installing the cracked api package on your mobile phone. Here is my code for reading and writing:


#import "ManagerFile.h"

@implementation ManagerFile

-(void)writeFile:(NSString *)file 
{ 
  // Create a file manager  
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  // Get the path  
  // Parameter NSDocumentDirectory To get that path  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0];// The path needed to go to the place   
  // Change to the directory to be operated on  
  [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; 
  // Create a file fileName File name, contents The contents of the file, if there is no content at first, can be set to nil , attributes The properties of the file, initially nil 
  // Get the file path  
  [fileManager removeItemAtPath:@"username" error:nil]; 
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; 
  // Create a data buffer  
  NSMutableData *writer = [[NSMutableData alloc] init]; 
  // Add a string to the buffer  
  [writer appendData:[file dataUsingEncoding:NSUTF8StringEncoding]]; 
  // Add additional data to the buffer  
  // Write buffered data to a file  
  [writer writeToFile:path atomically:YES]; 
  [writer release]; 
} 
-(NSString *)readFile 
{ 
  // Create a file manager  
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  // Get the path  
  // Parameter NSDocumentDirectory To get that path  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0];// The path needed to go to the place   
  // Change to the directory to be operated on  
  [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; 
  // Get the file path  
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; 
  NSData *reader = [NSData dataWithContentsOfFile:path]; 
  return [[NSString alloc] initWithData:reader 
                 encoding:NSUTF8StringEncoding]; 
} 
@end

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: