IOS 文件,数据的读写
获取到 file 到名字方法
NSArray *myPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSDocumentDirectory 是表示沙盒里 document 文件夹的列表
//NSDocumentionDirectory 是 获取 沙盒里 library 里 documention里的文件夹列表
NSString *path=[myPaths objectAtIndex:0]; // objectAtIndex : 为document 的文件列表
NSString *filename=[path stringByAppendingPathComponent:@"properties.plist"];
-(IBAction)save:(id)sender
{
NSArray *myPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[myPaths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"properties.plist"];
NSMutableArray *array=[[NSMutableArray alloc] init];
[array addObject:txt_name.text];
[array addObject:txt_class.text];
[array addObject:txt_id.text];
[array writeToFile:filename atomically:YES];
NSLog(@"come here");
}
-(IBAction)read:(id)sender
{
NSArray *mypaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[mypaths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"properties.plist"];
if([[NSFileManager defaultManager] fileExistsAtPath:filename])
{
NSLog(@"it‘s ok");
NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
txt_name.text=[array objectAtIndex:0];
txt_class.text=[array objectAtIndex:1];
txt_id.text=[array objectAtIndex:2];
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。