本地存储(数据持久化)
来源:互联网 发布:ubuntu新建文件夹 编辑:程序博客网 时间:2024/05/17 21:51
本地存数(数据持久化)主要是使用了沙盒
沙盒: 沙盒是iPhone工程进行文件操作有此工程对应的指定的位置, 不能逾越. 其中包含了四个文件: " documents, tmp, app, Library". 手动保存文件在documents文件夹里, NSUserDefaults保存的文件在tmp文件夹里.
1.获取sandBox下的文件夹路径:
/* 获取Documents文件夹路径 */
/* 使用函数:NSSearchPathForDirectoriesInDomains
* 参数1:所要查找的目录名字
* 参数2: 所要搜索的域(范围), iOS 在NSUserDomainMask查找
* 参数3: YES(显示为绝对路径), NO(显示为相对路径 ~/)
* 函数返回类型是一个数组
*/
NSArray *documentsArr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, NO);
/* 打印数组 */
NSLog(@"%@", documentsArr);
/* 从数组中将路径取出来 */
NSString *documentsPath = [documentsArr lastObject];
/* 打印字符串 */
NSLog(@"%@", documentsPath);
/* 获取Library 文件夹路径 */
NSArray *LibraryArr =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
NSString *LibraryPath = [LibraryArr lastObject];
NSLog(@"%@", LibraryPath);
/* 获取tmp文件夹路径
* 使用 NSTemporaryDirectory()函数
*/
NSString *tmpPath =NSTemporaryDirectory();
NSLog(@"%@", tmpPath);
/* 获取缓存文件夹路径 */
NSArray *CacherArr =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
NSString *CacherPath = [CacherArr lastObject];
NSLog(@"%@", CacherPath);
/* 连写 */
NSString *cacherPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];
NSLog(@"%@", cacherPath);
a. NSString 字符串写入文件
/* 1. 要存入本地的字符串 */
NSString *str = @"大连";
/* 2. 获取要保存的路径(哪一个文件夹), 文件保存到Documents文件夹中 */
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject];
/* 3. 创建文件的路径
* 在docPath路径基础上拼接文件的名字
*/
/* 方法一: */
NSString *strPath = [docPath stringByAppendingString:@"/location.txt"];
/* 方法二: 不用输入前面的"/"符号,会自动补全 */
NSString *strPath2 = [docPathstringByAppendingPathComponent:@"location.txt"];
NSLog(@"%@", strPath2);
/* 4. 将字符串写入文件 */
/*
* 参数1: 文件路径
* 参数2:原子性, YES(保存时,先保存在一个辅助文件中, 然后辅助文件替换原文件). NO(保存时,直接写入原文件)
* 参数3: 编码标准
* 参数4: 报错信息
*/
NSError *error = nil;
[str writeToFile:strPath2atomically:YESencoding:NSUTF8StringEncodingerror:&error];
b. NSArray 写入文件
/* 1. 要保存的数组 */
NSArray *arr =@[@"大连",@"沈阳",@"北京"];
/* 2. 创建文件路径
* 数组存入本地,若文件名没有后缀, 默认是XML格式
*/
NSString *arrayPath = [docPath stringByAppendingPathComponent:@"newArr.plist"];
/* 3. 写入 */
[arr writeToFile:arrayPath atomically:YES];
// NSLog(@"%@", arrayPath);
c. NSDictionary 存入本地
/* 1. 要保存的字典 */
NSDictionary *dic =@{@"name" :@"zhansan",
@"age" : @"18"};
/* 2. 创建文件路径 */
NSString *dicPath = [docPathstringByAppendingPathComponent:@"newDic.plist"];
/* 3. 写入 */
[dic writeToFile:dicPath atomically:YES];
// NSLog(@"%@", dicPath);
d. NSData 存入本地
/*如何将图片保存到本地
* 思路: 首先将图片转化为NSData对象, 然后将NSData对象保存到本地中
* 使用函数将jpg图片转化为NSData,函数中第二个参数是压缩比, 范围 0 - 1 , 1压缩比最小图像质量最高, 0压缩比最大图像质量最低
*/
NSData *imageData =UIImageJPEGRepresentation([UIImageimageNamed:@"0.jpg"],1);
/* 2. 创建文件路径 */
NSString *imagePath = [docPath stringByAppendingPathComponent:@"image.da"];
/* 3. 写入文件 */
[imageData writeToFile:imagePath atomically:YES];
NSLog(@"%@", imagePath);
3. 从文件中读取对象(即读文件)
/* 1. 找到文件路径(不是文件夹路径) */
NSString *documentsPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject];
NSString *readStrPath = [documentsPath2 stringByAppendingPathComponent:@"location.txt"];
/* 2. 创建对象(即读出内容) */
NSString *readStr = [NSStringstringWithContentsOfFile:readStrPath encoding:NSUTF8StringEncodingerror:&error];
NSLog(@"%@", readStr);
/* 数组 */
NSString *readArrPath = [documentsPath2 stringByAppendingPathComponent:@"newArr.plist"];
NSArray *readArr = [NSArrayarrayWithContentsOfFile:readArrPath];
NSLog(@"%@", readArr);
/* 字典 */
NSString *readDicPath = [documentsPath2 stringByAppendingPathComponent:@"newDic.plist"];
NSDictionary *readDic = [NSDictionarydictionaryWithContentsOfFile:readDicPath];
NSLog(@"%@", readDic);
/* NSData */
NSString *readImage = [documentsPath2 stringByAppendingPathComponent:@"image.da"];
NSData *readImageData = [NSDatadataWithContentsOfFile:readImage];
UIImage *Image = [UIImageimageWithData:readImageData];
NSLog(@"%@", Image);
UIImageView *imageView = [[UIImageViewalloc] initWithImage:Image];
imageView.frame = CGRectMake(100, 100, 300, 300);
[self.viewaddSubview:imageView];
[imageView release];
4. NSFileManager
/* 用于文件夹的管理,如:创建文件夹等,删除文件等 */
/* 1. 创建对象, 使用defaultManager 方法, 单例对象 */
NSFileManager *fileManager = [NSFileManagerdefaultManager];
/* 创建文件夹 ~/documents/file1 */
/* 创建文件夹路径 */
NSString *file1Dir = [documentsPath2 stringByAppendingPathComponent:@"file1"];
[fileManager createDirectoryAtPath:file1DirwithIntermediateDirectories:YESattributes:nilerror:&error];
// NSLog(@"%@", file1Dir);
/* 向file1文件夹中写入一个文件 */
NSString *string3 = @"北京";
NSString *string3Path = [file1Dir stringByAppendingPathComponent:@"beijing.txt"];
[string3 writeToFile:string3Pathatomically:YESencoding:NSUTF8StringEncodingerror:&error];
/*删除文件夹, 删除caches文件夹, caches文件夹删除之后,如果向这个文件夹写入东西, 或者程序重新运行,系统会自动创建caches文件夹 */
NSString *cacgesPath3 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];
[fileManager removeItemAtPath:cacgesPath3 error:&error];
/*查看某个文件夹中的内容 */
NSArray *contentArr = [fileManager contentsOfDirectoryAtPath:file1Dir error:&error];
NSLog(@"%@", contentArr);
5. 复杂对象写入文件
/* 定义一个Person类 */
Person *person1 = [[Personalloc] init];
person1.name = @"时琦狂三";
person1.sex = @"妹纸";
person1.age = @"不详";
person1.number = 10;
person1.address = @"dalian";
/* 归档 */
/*
* 1. 将复杂对象转化为NSData对象
* 2. NSData 写入文件中
*/
NSMutableData *data = [NSMutableDatadata];
NSKeyedArchiver *archiver1 = [[NSKeyedArchiveralloc] initForWritingWithMutableData:data];
/* 归档方法, 将person1 对象 归档 */
[archiver1 encodeObject:person1 forKey:@"per"];
/* 归档完成 */
[archiver1 finishEncoding];
/*创建一个保存文件的路径 */
NSString *perPath = [documentsPath2 stringByAppendingPathComponent:@"per.da"];
/* NSData对象写入文件 */
[data writeToFile:perPath atomically:YES];
/* 反归档(读取文件) */
/*
* 1. 读取NSData数据
* 2. 将NSData 数据转化为复杂对象
*/
NSData *perData = [NSDatadataWithContentsOfFile:perPath];
/* 创建反归档类对象 */
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:perData];
Person *per = [unarchiver decodeObjectForKey:@"per"];
/* 反归档结束 */
[unarchiver finishDecoding];
/* 验证 */
NSLog(@"%@", per.name);
NSLog(@"%@", per.sex);
NSLog(@"%@", per.address);
NSLog(@"%@", per.age);
NSLog(@"%d", per.number);
- 本地存储(数据持久化)
- 数据持久化--数据存储到本地
- ios实现 本地存储 数据持久化
- 初级数据持久化(本地存储)
- Android本地数据持久化:内部存储和外部存储
- iOS开发中的本地数据存储(持久化)
- ios本地持久化存储
- 数据持久化存储
- 数据持久化 ( 存储 )
- 持久化存储技术之本地存储
- iOS 本地数据持久化
- 数据存储:数据持久化
- 数据存储---数据持久化
- 【IOS-Swift】实战笔记——数据持久化之NSUserDefaults(本地轻量级数据存储)
- iphone数据持久化存储
- ios 数据持久化存储
- 数据的持久化存储
- 数据的持久化存储
- "数据结构翻转课堂"答疑实录——概述
- [uva 12412]A Typical Homework(师兄帮帮忙)
- iOS 图片压缩
- Vim命令使用方法
- SpringMVC(五)
- 本地存储(数据持久化)
- VC6:Could not execute: Access denied(Win32 error 5)
- Eclipse插件之jQueryWTP插件的安装方法
- 在HTML中使用JavaScript
- Java小技巧
- [总结]视音频编解码技术零基础学习方法- 理论
- hdu 1158 Employment Planning
- 01 程序启动工具
- 使用GET提交form