您的位置:首页 > 其它

本地存储

2015-10-02 17:02 399 查看
说到本地存储,就必须说存储到什么地方?储存方法有很多比如:CoreData,数据库,沙盒存储等等,这里讲的是将文件存放在沙盒文件中。

首先说下沙盒的目录结构:

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: