简单对象写入文件
//简单对象写入文件
//仅支持NSString,NSArray,NSDictionary,NSData
//获取document路径
// NSString
NSString * documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString * path = [documentPath stringByAppendingString:@"/document.txt"];
NSString * str = @"I LOVE MY IOS TEACHER!";
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",path);
//读取文件
NSString * content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",content);
//NSArray
NSArray * array = @[@"i",@"love",@"my",@"ios",@"teacher"];
[array writeToFile:path atomically:YES];
//读取文件
NSArray * array2 = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",array2);
NSArray * array3 = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"%@",array3);
//字典
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString * path2 = [cachePath stringByAppendingString:@"/cache.txt"];
NSDictionary * dic = @{@"name":@"zhang",@"gender":@"f"};
[dic writeToFile:path2 atomically:YES];
NSDictionary * dic2 = [NSDictionary dictionaryWithContentsOfFile:path2];
NSLog(@"%@",dic2);
//NSData
UIImage * image = [UIImage imageNamed:@"1"];
// UIImageView * imgView = [[UIImageView alloc] initWithImage:image];
// [self.view addSubview:imgView];
// NSLog(@"%@",imgView);
NSString * tempPath = NSTemporaryDirectory();
tempPath = [tempPath stringByAppendingString:@"/tmp.txt"];
//将图片转化成数据对象
NSData * data = UIImagePNGRepresentation(image);
//写入文件
[data writeToFile:tempPath atomically:YES];
NSLog(@"%@",tempPath);
//读取文件
NSData * _data = [NSData dataWithContentsOfFile:tempPath];
UIImage * _image = [UIImage imageWithData:_data];
NSLog(@"%@",_image);
// NSUserDefaults * usr = [NSUserDefaults standardUserDefaults];
// [usr setObject:@"tao" forKey:@"master"];
// [self test];
//NSFileManager
NSFileManager * file = [[NSFileManager alloc] init];
NSFileManager * file1 = [NSFileManager defaultManager];
NSString * homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingString:@"/A"];
[file createDirectoryAtPath:homePath withIntermediateDirectories:YES attributes:nil error:nil];
NSString * homePath2 = [homePath stringByAppendingString:@"/B"];
//新建文件夹
[file createDirectoryAtPath:homePath2 withIntermediateDirectories:YES attributes:nil error:nil];
//删除文件夹
[file removeItemAtPath:homePath2 error:nil];
//拷贝文件夹
// [file copyItemAtPath:homePath toPath:homePath2 error:nil];//(死循环了)
//移动
[file moveItemAtPath:homePath2 toPath:homePath error:nil];