//数据持久化 SandBox沙盒机制
//沙盒目录一般用来储存用户的缓存数据,该目录安全性非常高,别人很难获取
//参数1.获取沙盒中某个目录(文件夹)
//参数2.设置获取路径的搜索范围
//参数3.路径是否为绝对路径或者相对路径,yes是绝对路径
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
//获取document文件夹路径
NSString *doucumentPath = [array lastObject];
NSLog(@"document = %@", doucumentPath);
//Documents文件夹下用来存放用户数据,比如用户隐私信息,音频文件,视频文件,图片等缓存数据
//Library文件夹里面包含两个文件夹
//Caches:主要存放系统缓存数据文件
//Preference:主要存放系统配置文件
//以上三个文件夹有一个共同的特点就是当应用重新启动后,缓存的数据不会被清除,会一直缓存在本地;
//temp:临时文件夹,存放临时的数据,当应用重新启动时候,文件里的数据会被清空
//拼接路径,自动在路径之间加"/"
NSString *studentTxt = [doucumentPath stringByAppendingString:@"student.txt"];
//1.先拼接路径,有路径不代表路径下会自动生成文件
//2.创建数据,并写入到文件路径里
NSString *name = @"张三";
//写入文件数据
//参数1.设置写入的文件路径
//参数2.是否设置写入保护
//参数3.中文编码
//参数4.错误信息
BOOL result = [name writeToFile:studentTxt atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (result)
{
NSLog(@"写入成功");
}
else
{
NSLog(@"写入失败");
}
//往本地写入数组
NSString *arrayFilePath = [doucumentPath stringByAppendingString:@"array.xml"];
NSArray *stuArray = [NSArray arrayWithObjects:@"张三",@"李四",@"王五", nil];
BOOL result2 = [stuArray writeToFile:arrayFilePath atomically:YES];
if (result2)
{
NSLog(@"写入数组成功");
}
else
{
NSLog(@"写入数组失败");
}
NSArray *array2 = [NSArray arrayWithContentsOfFile:arrayFilePath];
NSLog(@"array2 = %@", array2);
//写入字典
NSString *dicFilePath = [doucumentPath stringByAppendingString:@"student.plist"];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"张三",@"name",@"男",@"gender",@"22",@"age", nil];
BOOL result3 = [dic writeToFile:dicFilePath atomically:YES];
if (result3)
{
NSLog(@"写入字典成功");
}
else
{
NSLog(@"写入字典失败");
}
NSDictionary *dic2 = [NSDictionary dictionaryWithContentsOfFile:dicFilePath];
NSLog(@"dic2 = %@", dic2);
//缓存自定义model模型数据
//需要使用ios中归档/反归档,专门来储存model数据到本地
#warning 归档第一步:让model签订NSCoding协议
#warning 归档第四步:对model进行归档操作
Student *stu = [[Student alloc] init];
stu.name = @"大水杯";
stu.gender = @"男";
stu.age = 23;
//aa(随便起名字)
NSString *archiverPath = [doucumentPath stringByAppendingString:@"student.aa"];
//归档类
BOOL result4 = [NSKeyedArchiver archiveRootObject:stu toFile:archiverPath];
if (result4)
{
NSLog(@"归档成功");
}
else
{
NSLog(@"归档失败");
}
//反归档取出model数据
Student *student = [NSKeyedUnarchiver unarchiveObjectWithFile:archiverPath];
NSLog(@"%@ %@ %ld", student.name, student.gender, student.age);
}
- (IBAction)createButton:(UIButton *)sender {
//NSFileManager 文件管理里器,是系统的单例(其他:NSUserDefaults 存数据, UIApplication 应用, UIScreen 屏幕, UIDevice 设备)
//1.获到document路径
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
NSString *MWKPath = [filePath stringByAppendingPathComponent:@"MWK"];
//创建文件管理器对象
NSFileManager *fileManager = [NSFileManager defaultManager];
//创建文件夹
//参数1.文件夹路径
//参数2.设置yes,中间路径
//参数3.设置属性,nil
//参数4.错误信息
BOOL result = [fileManager createDirectoryAtPath:MWKPath withIntermediateDirectories:YES attributes:nil error:nil];
if (result)
{
NSLog(@"文件夹创建成功");
}
else
{
NSLog(@"文件夹创建失败");
}
NSString *miwenkaiTxt = [MWKPath stringByAppendingPathComponent:@"miwenkai.txt"];
NSLog(@"%@", miwenkaiTxt);
NSString *name = @"米文凯";
[name writeToFile:miwenkaiTxt atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
- (IBAction)CopyButton:(UIButton *)sender {
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
NSString *LZSPath = [filePath stringByAppendingPathComponent:@"LZS"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:LZSPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *liuZSTxt = [LZSPath stringByAppendingPathComponent:@"liuzhishan.txt"];
//总结:不管是创建文件夹还是文件,首先必须要拼接路径
//1.如果是创建文件夹,直接使用NSFileManager文件管理器
//2.如果是创建文件,直接将数据写入到路径,即可创建文件
NSString *miwenkai = [filePath stringByAppendingPathComponent:@"MWK"];
NSString *miwenkaiTxt = [miwenkai stringByAppendingPathComponent:@"miwenkai.txt"];
BOOL result = [fileManager copyItemAtPath:miwenkaiTxt toPath:liuZSTxt error:nil];
if (result)
{
NSLog(@"拷贝成功");
}
else
{
NSLog(@"拷贝失败");
}
//拼接BaiHao文件夹
NSString *baiHao = [filePath stringByAppendingPathComponent:@"BaiHao"];
[fileManager createDirectoryAtPath:baiHao withIntermediateDirectories:YES attributes:nil error:nil];
//拼接baihao.tex文件
NSString *baihaoTxt = [baiHao stringByAppendingPathComponent:@"baihao.txt"];
BOOL result1 = [fileManager moveItemAtPath:liuZSTxt toPath:baihaoTxt error:nil];
if (result1)
{
NSLog(@"移动成功");
}
else
{
NSLog(@"移动失败");
}
BOOL removeResult = [fileManager removeItemAtPath:miwenkai error:nil];
if (removeResult)
{
NSLog(@"必须成功");
}
BOOL existResult = [fileManager fileExistsAtPath:baihaoTxt];
if (existResult)
{
NSLog(@"已存在");
}
//针对数组里存放的是model对象,怎么雪茹本地文件夹里
//1.先将model实现NSCoding协议,实现编码解码协议方法
//2.将生成的model对象存入数组中
//3.对数组进行归档/反归档
}
Student.h
@interface Student : NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *gender;
@property(nonatomic,assign)NSInteger age;
Student.m
#warning 归档第二步:对model属性进行编码
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.gender forKey:@"gender"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
#warning 归档第三步:对model属性进行解码
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
self.name = [aDecoder decodeObjectForKey:@"name"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
self.age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
iOS序列化与反序列化
最新推荐文章于 2023-06-24 18:05:09 发布