IOS 文件读写操作详解及简单实例

来自:互联网
时间:2020-05-26
阅读:
免费资源网 - https://freexyz.cn/

iPhone 文件读写操作

1,写文件操作

- (IBAction)btnWrite:(id)sender {
  //创建文件管理器
  NSFileManager *fileManager = [NSFileManager defaultManager];
  //获取路径
  //参数 要获取的哪种路径
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentDirectory = [paths objectAtIndex:0];//去除需要的路径
  //更改到操作的目录下
  [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]];
   //创建文件fileName文件名称,contents文件内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
  [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];
  //删除待删除文件
  [fileManager removeItemAtPath:@"createNewFile" error:nil];
  //获取文件路径
  NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"];
  NSLog(@"path == %@",path);
  //待写入的数据
  NSString *temp = @"Hello world";
  int data0 = 1000000;
  float data2 = 23.23f;
  //创建数据缓冲
  NSMutableData *writer = [[NSMutableData alloc]init];
  //将字符串添加到缓冲中
  [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,读文件操作

- (IBAction)btnRead:(id)sender {
  //创建文件管理器
  NSFileManager *fileManager = [NSFileManager defaultManager];
  //获取路径
  //参数 要获取的哪种路径
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentDirectory = [paths objectAtIndex:0];//去除需要的路径
  //更改到操作的目录下
  [fileManager changeCurrentDirectoryPath:[documentDirectory stringByExpandingTildeInPath]];
   //创建文件fileName文件名称,contents文件内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
  [fileManager createFileAtPath:@"fileName" contents:nil attributes:nil];
  //删除待删除文件
  [fileManager removeItemAtPath:@"createNewFile" error:nil];
  //获取文件路径
  NSString *path = [documentDirectory stringByAppendingPathComponent:@"fileName"];
  NSLog(@"path == %@",path);
  //待写入的数据
  NSString *temp = @"Hello world";
  int data0 = 1000000;
  float data2 = 23.23f;
  //创建数据缓冲
  NSMutableData *writer = [[NSMutableData alloc]init];
  //将字符串添加到缓冲中
  [writer appendData:[temp dataUsingEncoding:NSUTF8StringEncoding]];
  [writer appendBytes:&data0 length:sizeof(data0)];
  [writer appendBytes:&data2 length:sizeof(data2)];
   
  [writer writeToFile:path atomically:YES];
  [writer release];
  
  //读取操作
  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实现文件的读写操作

iphone可以很容易的实现文件的读写,但是如果你没有使用苹果公司99美元开发者sdk的话,你利用破解打出来的api包,安装到手机上不能把文件写入手机.下面是我实现读写的代码:

#import "ManagerFile.h"

@implementation ManagerFile

-(void)writeFile:(NSString *)file 
{ 
  //创建文件管理器 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  //获取路径 
  //参数NSDocumentDirectory要获取那种路径 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径  
  //更改到待操作的目录下 
  [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; 
  //创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil 
  //获取文件路径 
  [fileManager removeItemAtPath:@"username" error:nil]; 
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; 
  //创建数据缓冲 
  NSMutableData *writer = [[NSMutableData alloc] init]; 
  //将字符串添加到缓冲中 
  [writer appendData:[file dataUsingEncoding:NSUTF8StringEncoding]]; 
  //将其他数据添加到缓冲中 
  //将缓冲的数据写入到文件中 
  [writer writeToFile:path atomically:YES]; 
  [writer release]; 
} 
-(NSString *)readFile 
{ 
  //创建文件管理器 
  NSFileManager *fileManager = [NSFileManager defaultManager]; 
  //获取路径 
  //参数NSDocumentDirectory要获取那种路径 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  NSString *documentsDirectory = [paths objectAtIndex:0];//去处需要的路径  
  //更改到待操作的目录下 
  [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]]; 
  //获取文件路径 
  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"username"]; 
  NSData *reader = [NSData dataWithContentsOfFile:path]; 
  return [[NSString alloc] initWithData:reader 
                 encoding:NSUTF8StringEncoding]; 
} 
@end

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

免费资源网 - https://freexyz.cn/
返回顶部
顶部