本地存储
2015-10-02 17:02
399 查看
说到本地存储,就必须说存储到什么地方?储存方法有很多比如:CoreData,数据库,沙盒存储等等,这里讲的是将文件存放在沙盒文件中。
首先说下沙盒的目录结构:
1 Documents 文件夹:存储用户的信息,icloud会同步此文件夹下的文件
2 Lidrary 文件夹有2个文件夹:1 Caches文本夹:缓存文件夹,清理缓存,就是指此文件夹
2 preferences文件夹:一般存放用户的偏好设置等文件
3 tmp文件夹:临时文件夹,存放临时文件
一般我们存放图片,文字是存放在Caches文件夹中,其他的偏好设置比如说:夜间模式的值(一键换肤)就存放在preferences文件夹。
//使用函数:NSSearchPathForDirectoriesInDomains
//参数1:所要查找的目录名字
//参数2:所要收索的域(范围),iOS在NSUserDomainMask查找
//参数3:YES(显示绝对路径),NO(显示相对路径
~/)
//函数返回类型是一个数组
NSArray *documentsArr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
NSLog(@"%@",documentsArr);
//从数组中将路径取出来
NSString *doccumentsPath = [documentsArrlastObject];
//打印字符串
NSLog(@"%@",doccumentsPath);
//获取Library文件路径
NSArray *LibaryArr =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,
YES);
NSString *LibaryPath = [LibaryArrlastObject];
NSLog(@"%@",LibaryPath);
//获取tmp文件夹路径
NSLog(@"%@",NSTemporaryDirectory());
NSString *str =
@"大连";
// 2. 获取要保存的路径(哪一个文件夹),文件保存到Documents
文件中
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];
// 3. 创建文件的路径
// 在docPath路径基础上拼接文件的名字
// 方法一:
//NSString *strPath = [docPath stringByAppendingString:@"/location.txt"];
//方法二:
不用输出前面的 / 符号,系统自动补全
NSString *strPath2 = [docPath
stringByAppendingPathComponent:@"location.txt"];
NSLog(@"%@",strPath2);
// 4. 将字符串写入文件中
// 参数1:文件的路径
// 参数2:原子性,yes(保存时,先保存在一个辅助文件中,然后辅助文件替换原文件) no(保存时,直接写入原文件)
// 参数3:编码标准
// 参数4:错误信息
NSError *error =
nil;
[str writeToFile:strPath2
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
// 1 要保存的数组
NSArray *array =
@[@"大连",@"沈阳",@"北京"];
// 2 传建文件路径
// 数组存入本地,若文件名没有后缀,默认是XML格式
NSString *arrayPath = [docPath
stringByAppendingPathComponent:@"newArr.plist"];
// 3 写入文件
[array writeToFile:arrayPath
atomically:YES];
// 1 要存入的字典
NSDictionary *dic =
@{@"name":@"zhangs",@"age":@"18"};
// 2 创建文件路径
NSString *dicPath = [docPath
stringByAppendingPathComponent:@"dic.plist"];
// 3 写入文件
[dic writeToFile:dicPath
atomically:YES];
//如何将图片保存到本地
//思路:首先将图片转化为NSData对象,然后将NSData对象保存到本地文件中
//使用函数将JPG图片转化为NSData,函数中的第二个参数是压缩比,范围是0~1,1压缩比最小图象质量高,
0 压缩比最大,图片质量低
NSData *imaheData =
UIImageJPEGRepresentation([UIImage
imageNamed:@"huo.jpg"],
1);
// 创建文件路径
NSString *imagePath = [docPath
stringByAppendingPathComponent:@"imaheData.da"];
//写入文件
[imaheData writeToFile:imagePath
atomically:YES];
NSString *documenPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];
NSString *readStrPath = [documenPath
stringByAppendingPathComponent:@"location.txt"];
// 2创建对象(即读出内容)
NSString *readStr = [NSString
stringWithContentsOfFile:readStrPath
encoding:NSUTF8StringEncoding
error:&error];
NSLog(@"readStr :%@",readStr);
//数组
NSString *readArrPath = [documenPath
stringByAppendingPathComponent:@"newArr.plist"];
NSArray *readArr = [NSArray
arrayWithContentsOfFile:readArrPath];
NSLog(@"%@",readArr);
//字典
NSString *dicPath1 = [doccumentsPath
stringByAppendingPathComponent:@"dic.plist"];
NSDictionary *drric = [NSDictionary
dictionaryWithContentsOfFile:dicPath1];
NSLog(@"%@",drric);
//NSData
NSString *datastr = [doccumentsPath
stringByAppendingPathComponent:@"imaheData.da"];
NSData *dataa = [NSData
dataWithContentsOfFile:datastr];
UIImage *image = [UIImage
imageWithData:dataa];
NSLog(@"%@",image);
方法,单例对象
NSFileManager *fileManager = [NSFileManager
defaultManager];
//创建文件夹 ~/documents/file1
//创建文件夹路径
NSString *file1Dir = [doccumentsPath
stringByAppendingPathComponent:@"file1"];
[fileManager createDirectoryAtPath:file1Dir
withIntermediateDirectories:YES
attributes:nil
error:&error];
NSLog(@"%@",file1Dir);
//向file1
文件夹中写入一个文件
NSString *filestr =
@"日了狗了";
NSString *filestr2 = [file1Dir
stringByAppendingPathComponent:@"hehe.txt"];
[filestr writeToFile:filestr2
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
//删除文件夹,
删除caches文件夹,caches文件夹删除之后,如果向这个文件夹写入东西,或者程序重新运行,系统会自动创建caches文件夹
NSString *cachesPath3 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)
lastObject];
[fileManager removeItemAtPath:cachesPath3
error:&error];
//查看某个文件夹中的内容
NSArray *arrayy = [fileManager
contentsOfDirectoryAtPath:file1Dir
error:&error];
NSLog(@"%@",arrayy);
在.m中写一下2个协议方法:
// 实现 NSCoding协议方法,
两个协议方法必须实现
// 复杂对象归档时,需要调用的方法,目的是将所有属性进行编码
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name
forKey:@"name"];
[aCoder encodeObject:self.sex
forKey:@"sex"];
[aCoder encodeObject:self.age
forKey:@"age"];
[aCoder encodeInt:self.number
forKey:@"number"];
}
// 复杂对象反归档时调用,目的是
解码后赋值给属性
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super
init];
if (self) {
self.name = [aDecoder
decodeObjectForKey:@"name"];
self.sex = [aDecoder
decodeObjectForKey:@"sex"];
self.age = [aDecoder
decodeObjectForKey:@"age"];
self.number = [aDecoder
decodeIntForKey:@"number"];
}
return
self;
}
然后将需要存储的Model类归档和反归档
//定义一个Person类
Person *person1 = [[Person
alloc] init];
person1.name =
@"zhangs";
person1.sex =
@"male";
person1.age =
@"20";
person1.number =
10;
//归档
// 1 将复杂对象转化为NSdata对象
// 2 NSData 写入文件中
NSMutableData *data = [NSMutableData
data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver
alloc] initForWritingWithMutableData:data];
//归档方法,将pereson1
对象归档
[archiver encodeObject:person1
forKey:@"per"];
//归档完成
[archiver finishEncoding];
//创建一个保存文件的路径
NSString *perPath = [doccumentsPath
stringByAppendingPathComponent:@"per.da"];
[data writeToFile:perPath
atomically:YES];
#pragma mark - 反归档(读文件)
// 1 读取NSData数据
// 2 将NSData
数据转换为复杂对象
NSData *perData = [NSData
dataWithContentsOfFile:perPath];
//创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver
alloc] initForReadingWithData:perData];
Person *per = [unarchiver
decodeObjectForKey:@"per"];
//反归档结束
[unarchiver finishDecoding];
//验证
NSLog(@"%@",per.name);
NSLog(@"%@",per.sex);
NSLog(@"%@",per.age);
NSLog(@"%d",per.number);
}
首先说下沙盒的目录结构:
1 Documents 文件夹:存储用户的信息,icloud会同步此文件夹下的文件
2 Lidrary 文件夹有2个文件夹:1 Caches文本夹:缓存文件夹,清理缓存,就是指此文件夹
2 preferences文件夹:一般存放用户的偏好设置等文件
3 tmp文件夹:临时文件夹,存放临时文件
一般我们存放图片,文字是存放在Caches文件夹中,其他的偏好设置比如说:夜间模式的值(一键换肤)就存放在preferences文件夹。
我们先要获得沙盒文件夹的路径
//获取Documents文件夹路径//使用函数:NSSearchPathForDirectoriesInDomains
//参数1:所要查找的目录名字
//参数2:所要收索的域(范围),iOS在NSUserDomainMask查找
//参数3:YES(显示绝对路径),NO(显示相对路径
~/)
//函数返回类型是一个数组
NSArray *documentsArr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
NSLog(@"%@",documentsArr);
//从数组中将路径取出来
NSString *doccumentsPath = [documentsArrlastObject];
//打印字符串
NSLog(@"%@",doccumentsPath);
//获取Library文件路径
NSArray *LibaryArr =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,
YES);
NSString *LibaryPath = [LibaryArrlastObject];
NSLog(@"%@",LibaryPath);
//获取tmp文件夹路径
NSLog(@"%@",NSTemporaryDirectory());
知道了沙盒路径,现在我们进行简单对象写入
// 1. 要存入本地的字符串NSString *str =
@"大连";
// 2. 获取要保存的路径(哪一个文件夹),文件保存到Documents
文件中
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];
// 3. 创建文件的路径
// 在docPath路径基础上拼接文件的名字
// 方法一:
//NSString *strPath = [docPath stringByAppendingString:@"/location.txt"];
//方法二:
不用输出前面的 / 符号,系统自动补全
NSString *strPath2 = [docPath
stringByAppendingPathComponent:@"location.txt"];
NSLog(@"%@",strPath2);
// 4. 将字符串写入文件中
// 参数1:文件的路径
// 参数2:原子性,yes(保存时,先保存在一个辅助文件中,然后辅助文件替换原文件) no(保存时,直接写入原文件)
// 参数3:编码标准
// 参数4:错误信息
NSError *error =
nil;
[str writeToFile:strPath2
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
// 1 要保存的数组
NSArray *array =
@[@"大连",@"沈阳",@"北京"];
// 2 传建文件路径
// 数组存入本地,若文件名没有后缀,默认是XML格式
NSString *arrayPath = [docPath
stringByAppendingPathComponent:@"newArr.plist"];
// 3 写入文件
[array writeToFile:arrayPath
atomically:YES];
// 1 要存入的字典
NSDictionary *dic =
@{@"name":@"zhangs",@"age":@"18"};
// 2 创建文件路径
NSString *dicPath = [docPath
stringByAppendingPathComponent:@"dic.plist"];
// 3 写入文件
[dic writeToFile:dicPath
atomically:YES];
//如何将图片保存到本地
//思路:首先将图片转化为NSData对象,然后将NSData对象保存到本地文件中
//使用函数将JPG图片转化为NSData,函数中的第二个参数是压缩比,范围是0~1,1压缩比最小图象质量高,
0 压缩比最大,图片质量低
NSData *imaheData =
UIImageJPEGRepresentation([UIImage
imageNamed:@"huo.jpg"],
1);
// 创建文件路径
NSString *imagePath = [docPath
stringByAppendingPathComponent:@"imaheData.da"];
//写入文件
[imaheData writeToFile:imagePath
atomically:YES];
从文件中读取对象
// 1找到文件路径(不是文件夹路径)NSString *documenPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];
NSString *readStrPath = [documenPath
stringByAppendingPathComponent:@"location.txt"];
// 2创建对象(即读出内容)
NSString *readStr = [NSString
stringWithContentsOfFile:readStrPath
encoding:NSUTF8StringEncoding
error:&error];
NSLog(@"readStr :%@",readStr);
//数组
NSString *readArrPath = [documenPath
stringByAppendingPathComponent:@"newArr.plist"];
NSArray *readArr = [NSArray
arrayWithContentsOfFile:readArrPath];
NSLog(@"%@",readArr);
//字典
NSString *dicPath1 = [doccumentsPath
stringByAppendingPathComponent:@"dic.plist"];
NSDictionary *drric = [NSDictionary
dictionaryWithContentsOfFile:dicPath1];
NSLog(@"%@",drric);
//NSData
NSString *datastr = [doccumentsPath
stringByAppendingPathComponent:@"imaheData.da"];
NSData *dataa = [NSData
dataWithContentsOfFile:datastr];
UIImage *image = [UIImage
imageWithData:dataa];
NSLog(@"%@",image);
用于文件夹的管理,如:创建文件夹,删除文件夹等
// 1 创建对象,使用defaultManager方法,单例对象
NSFileManager *fileManager = [NSFileManager
defaultManager];
//创建文件夹 ~/documents/file1
//创建文件夹路径
NSString *file1Dir = [doccumentsPath
stringByAppendingPathComponent:@"file1"];
[fileManager createDirectoryAtPath:file1Dir
withIntermediateDirectories:YES
attributes:nil
error:&error];
NSLog(@"%@",file1Dir);
//向file1
文件夹中写入一个文件
NSString *filestr =
@"日了狗了";
NSString *filestr2 = [file1Dir
stringByAppendingPathComponent:@"hehe.txt"];
[filestr writeToFile:filestr2
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
//删除文件夹,
删除caches文件夹,caches文件夹删除之后,如果向这个文件夹写入东西,或者程序重新运行,系统会自动创建caches文件夹
NSString *cachesPath3 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)
lastObject];
[fileManager removeItemAtPath:cachesPath3
error:&error];
//查看某个文件夹中的内容
NSArray *arrayy = [fileManager
contentsOfDirectoryAtPath:file1Dir
error:&error];
NSLog(@"%@",arrayy);
复杂对象写入文件
首先你先要在你的model类中添加协议方法:在.h中添加 NSCoding 协议方法在.m中写一下2个协议方法:
// 实现 NSCoding协议方法,
两个协议方法必须实现
// 复杂对象归档时,需要调用的方法,目的是将所有属性进行编码
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name
forKey:@"name"];
[aCoder encodeObject:self.sex
forKey:@"sex"];
[aCoder encodeObject:self.age
forKey:@"age"];
[aCoder encodeInt:self.number
forKey:@"number"];
}
// 复杂对象反归档时调用,目的是
解码后赋值给属性
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super
init];
if (self) {
self.name = [aDecoder
decodeObjectForKey:@"name"];
self.sex = [aDecoder
decodeObjectForKey:@"sex"];
self.age = [aDecoder
decodeObjectForKey:@"age"];
self.number = [aDecoder
decodeIntForKey:@"number"];
}
return
self;
}
然后将需要存储的Model类归档和反归档
//定义一个Person类
Person *person1 = [[Person
alloc] init];
person1.name =
@"zhangs";
person1.sex =
@"male";
person1.age =
@"20";
person1.number =
10;
//归档
// 1 将复杂对象转化为NSdata对象
// 2 NSData 写入文件中
NSMutableData *data = [NSMutableData
data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver
alloc] initForWritingWithMutableData:data];
//归档方法,将pereson1
对象归档
[archiver encodeObject:person1
forKey:@"per"];
//归档完成
[archiver finishEncoding];
//创建一个保存文件的路径
NSString *perPath = [doccumentsPath
stringByAppendingPathComponent:@"per.da"];
[data writeToFile:perPath
atomically:YES];
#pragma mark - 反归档(读文件)
// 1 读取NSData数据
// 2 将NSData
数据转换为复杂对象
NSData *perData = [NSData
dataWithContentsOfFile:perPath];
//创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver
alloc] initForReadingWithData:perData];
Person *per = [unarchiver
decodeObjectForKey:@"per"];
//反归档结束
[unarchiver finishDecoding];
//验证
NSLog(@"%@",per.name);
NSLog(@"%@",per.sex);
NSLog(@"%@",per.age);
NSLog(@"%d",per.number);
}
相关文章推荐
- GT I9082 刷机的过程
- WindowState注意事项
- 关情纸尾-----Quartz2D-上下文状态栈与上下文的矩阵操作
- 浏览器缓存浅析(一)
- Fast paging with MongoDB
- pat1008Elevator (20)
- Win32 线程知识点梳理五
- 《程序猿》约稿:在多任务的时间管理,规划的目标和任务分解
- ArrayList的实现原理分析
- mysql主备库数据不一致的原因和解决方案
- poj--2251--Dungeon Master
- Android 按键消息处理
- 我心里的用户体验
- 关于EMGU CV的那些事——3.不同角点检测算法比较
- POJ: Exponentiation
- LinkedHashSet实现原理分析
- ESP8266-01细玩笔记(二)
- CodeForces 414A Mashmokh and Numbers
- Node.js第三方模块之htmlparser2
- iOS8 沙盒路径变化特性