- (void)viewDidLoad {
[super viewDidLoad];
NSArray *documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = documentsArr[0];
NSString *path = [documentPath stringByAppendingPathComponent:@"person.text"];
NSLog(@"path = %@", path);
// 写入字典
NSString *dicPath = [documentPath stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = @{@"name":@"1",@"age":@"2"};
[dic writeToFile:dicPath atomically:YES];
// 写入字符串
NSString *strPath = [documentPath stringByAppendingPathComponent:@"str.plist"];
NSString *str = @"这是字符串";
[str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
[self delete];
[self read];
}
- (void)read{
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [docPath objectAtIndex:0];
// 写入数组
NSString *readPath = [documentsPath stringByAppendingPathComponent:@"person.text"];
NSArray *array = @[@"1", @"2", @"3"];
[array writeToFile:readPath atomically:YES];
// 读取数组
NSArray *array1 = [NSArray arrayWithContentsOfFile:readPath];
NSLog(@"%@", array1);
// 读出字典
NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
NSLog(@"dic = %@", dic);
// 读出字符串
NSString *strPath = [documentsPath stringByAppendingPathComponent:@"str.plist"];
NSString *str1 = [[NSString alloc] initWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str1);
}
- (void)delete
{
// NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsPath = [docPath objectAtIndex:0];
// NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];
// NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
// NSLog(@"%@", dic);
// NSString *str = dic[@"name"];
// 删除文件
NSFileManager *fileManeger = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"zidian.plist"];
// BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
// if (!blHave) {
// return ;
// }else {
// BOOL blDele= [fileManeger removeItemAtPath:uniquePath error:nil];
// if (blDele) {
// NSLog(@"dele success");
// }else {
// NSLog(@"dele fail");
// }
//
// }
// 结果是布尔类型
BOOL blDele= [fileManeger removeItemAtPath:uniquePath error:nil];
// 此时已经文件是空
NSLog(@"%@", blDele?@"YES":@"NO");
}