您的位置:首页 > 其它

SaveData

2015-09-16 21:12 162 查看
沙盒:其实对于每一个应用程序,都有唯一的一个本地文件与之对应,名字由系统随机生成.这个文件就是沙盒
沙盒机制:沙盒机制其实就是,对每一个应用程序的资源起到一个保护作用,当前程序不允许访问其他程序的资源,其他程序也不允许访问当前程序的资源.
对于每一个应用程序的沙盒文件中都包含以下文件:
1.documents:用来存储持久化数据文件.如果我们想对一个文件进行长久存储,就放在该文件夹下
2.libary:
(a)caches:缓存文件,存放已经下载完成的视频,音频,图片等等.一般我们会在该文件夹下创建Images,Audioes,Videoes等文件,分别存放图片,视频,音频
(b)prefrences:用于存储用户的偏好设置,比如用于判别程序是否是第一次启动的plist文件就放在该文件夹下
3.tmp:存放未下载完成的视频,音频等.一般我们会将下载完成的视频,音频再手动移动到caches中
XXX.app :
应用程序的包,应用程序的资源都来源于包,而包也是我们上传到appStore以及用户从appStore下载的文件.对于包内的资源我们不能进行修改,更不能删除

另外,对于以上文件都是由系统创建,不允许随意删除修改我们只能删除修改自己创建的文件

//获取沙盒路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

NSLog(@"%@",documentsPath);

//获取包路径
NSString *appPath = [[NSBundle mainBundle] resourcePath];
NSLog(@"%@",appPath);


//写入
- (IBAction)writeAction:(id)sender {

#pragma mark - 字符串写入本地
//获取字符串
NSString *str1 = _Tf1.text;
NSString *str2 = _Tf2.text;
//获取documents文件路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//拼接文件路径
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"test.TXT"];
//    NSString *filePath = [documentsPath stringByAppendingString:@"/test.TXT"];

//写入
[str1 writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//atomically:YES  读取的的数据放到临时文件中,完全读完才显示.如果断开连接,完全不显示
//atomically:NO 读取了多少显示多少
#pragma mark - 数组写入本地

//数组
NSArray *arr = @[str1,str2];

//拼接文件路径
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"arr.plist"];

//写入
[arr writeToFile:filePath atomically:YES];

#pragma mark - 字典的写入本地
//字典
NSDictionary *dic = @{@"tf1":_Tf1.text ,@"tf2":_Tf2.text};
//拼接
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"dic.plist"];
//写入
[dic writeToFile:filePath atomically:YES];

#pragma mark - data写入本地

NSData *data = [str1 dataUsingEncoding:NSUTF8StringEncoding];

//文件拼接
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"data.TXT"];
[data writeToFile:filePath atomically:YES];

}
//读取
- (IBAction)readAction:(id)sender {
#pragma mark - 字符串读取
//获取文件路径
//获取document文件路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//拼接文件路径
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"test.TXT"];
//展示
self.Tf2.text = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

#pragma mark - 字符串读取

//拼接文件路径
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"arr.json"];
//读取
NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
//展示
self.Tf1.text = arr[0];
self.Tf2.text = arr[1];

#pragma mark - 字典读取

NSString *filePath = [documentsPath stringByAppendingPathComponent:@"dic.plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
//展示
self.Tf1.text = [dic valueForKey:@"tf1"];
self.Tf2.text = [dic valueForKey:@"tf2"];

#pragma mark - data读取

NSString *filePath = [documentsPath stringByAppendingPathComponent:@"data.TXT"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
//展示
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.Tf2.text = str;

}
//创建
- (IBAction)creatAction:(id)sender {

//获取documents文件路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//拼接文件夹路径
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Image"];

//创建文件管理对象
NSFileManager *manager = [NSFileManager defaultManager];
//创建
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
//移动
- (IBAction)moveAction:(id)sender {

//将Image 移动到caches中
//获取oldPath
//获取tmp路径
NSString *tempPath = NSTemporaryDirectory();
//拼接
NSString *oldPath = [tempPath stringByAppendingPathComponent:@"Image"];

//newPath
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
//拼接
NSString *newPath = [cachesPath stringByAppendingPathComponent:@"Image"];

//创建文件管理对象
NSFileManager *manager = [NSFileManager defaultManager];

//移动
[manager moveItemAtPath:oldPath toPath:newPath error:nil];

}
//复制
- (IBAction)copyAction:(id)sender {

//获取tmp文件路径
NSString *tmp = NSTemporaryDirectory();

//2.文件拼接
NSString *newPath = [tmp stringByAppendingPathComponent:@"Image"];

//获取documents文件路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//文件拼接
NSString *oldPath = [documentsPath stringByAppendingPathComponent:@"Image"];

//创建文件管理对象
NSFileManager *manager = [NSFileManager defaultManager];

//复制
[manager copyItemAtPath:oldPath toPath:newPath error:nil];

}
//删除
- (IBAction)removeAction:(id)sender {
//获取caches文件路径

NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];

//拼接
NSString *filePath = [cachesPath stringByAppendingPathComponent:@"Image"];
//创建文件管理对象
NSFileManager *manger = [NSFileManager defaultManager];
//删除
[manger removeItemAtPath:filePath error:nil];

}
//是否存在
- (IBAction)existAction:(id)sender {

//获取caches文件路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];
//拼接
NSString *filePath = [cachesPath stringByAppendingPathComponent:@"Image"];
//创建文件管理对象
NSFileManager *manger = [NSFileManager defaultManager];

//判断
BOOL isExist = [manger fileExistsAtPath:filePath];

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

if (isExist) {
alertView.message = @"文件存在";
}else
{
alertView.message = @"文件不存在";
}
[alertView show];

}


//归档
- (IBAction)archiverAction:(id)sender {

//创建学生对象
Student *stu = [[Student alloc] init];
stu.name = _nameTf.text;
stu.sex = _sexTf.text;
stu.age = _ageTf.text;

//归档操作
//1.创建可变data
NSMutableData *muData = [NSMutableData data];
//2.创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:muData];

//3.归档
[archiver encodeObject:stu forKey:@"JingJing"];
//4.归档完成
[archiver finishEncoding];

//本地持久化
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
//拼接
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"student.data"];
//写入
[muData writeToFile:filePath atomically:YES];

[stu release];
[archiver release];

}
//反归档
- (IBAction)unarchiverAction:(id)sender {

//获取documents文件路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"student.data"];

//反归档
//1.获取可变data
NSMutableData *muData = [NSMutableData dataWithContentsOfFile:filePath];
//2.创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:muData];
//3.反归档
Student *stu = [unarchiver decodeObjectForKey:@"JingJing"];
//4.结束反归档
[unarchiver finishDecoding];

_nameTf.text = stu.name;
_sexTf.text = stu.sex;
_ageTf.text = stu.age;

}


-(void)encodeWithCoder:(NSCoder *)aCoder
{
//对实例变量进行归档
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_sex forKey:@"sex"];
[aCoder encodeObject:_age forKey:@"age"];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
//对实例变量进行反归档
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
self.age = [aDecoder decodeObjectForKey:@"age"];
}
return self;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: