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 //What kind of path to get NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0];//Remove the required path //Change to the operation directory [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]]; //Create file fileName file name, contents file content, if there is no content at the beginning, it can be set to nil, attributes file attributes, initially nil [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil]; //Delete the file to be deleted [fileManager removeItemAtPath:@"createNewFile" error:nil]; //Get 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 data buffering NSMutableData *writer = [[NSMutableData alloc]init]; //Add string to 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 //What kind of path to get NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0];//Remove the required path //Change to the operation directory [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]]; //Create file fileName file name, contents file content, if there is no content at the beginning, it can be set to nil, attributes file attributes, initially nil [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil]; //Delete the file to be deleted [fileManager removeItemAtPath:@"createNewFile" error:nil]; //Get 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 data buffering NSMutableData *writer = [[NSMutableData alloc]init]; //Add string to 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); // = gData2; }
Iphone implements read and write files
iPhone can easily implement file reading and writing, but if you do not use Apple's $99 developer SDK, you can use the cracked API package to install it on your phone and cannot write files to your phone. Here is the code I implemented to read and write:
#import "" @implementation ManagerFile -(void)writeFile:(NSString *)file { //Create a file manager NSFileManager *fileManager = [NSFileManager defaultManager]; //Get the path //What kind of path should I get from the NSDocumentDirectory parameter NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];//The path required for the place //Change to the directory to be operated [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; //Create file fileName file name, contents file contents, if there is no content at the beginning, it can be set to nil, attributes file attributes, initially nil //Get file path [fileManager removeItemAtPath:@"username" error:nil]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; //Create data buffering NSMutableData *writer = [[NSMutableData alloc] init]; //Add string to buffer [writer appendData:[file dataUsingEncoding:NSUTF8StringEncoding]]; //Add other data to buffer //Write the buffered data into the file [writer writeToFile:path atomically:YES]; [writer release]; } -(NSString *)readFile { //Create a file manager NSFileManager *fileManager = [NSFileManager defaultManager]; //Get the path //What kind of path should I get from the NSDocumentDirectory parameter NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];//The path required for the place //Change to the directory to be operated [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; //Get file path NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; NSData *reader = [NSData dataWithContentsOfFile:path]; return [[NSString alloc] initWithData:reader encoding:NSUTF8StringEncoding]; } @end
Thank you for reading, I hope it can help you. Thank you for your support for this site!