iOS进阶(数据处理之文件读写)
1.沙盒的定义
每一个iOS程序都会为自己创建一个文件系统目录,这个独立、封闭、安全的空间就叫做沙盒
2.Documents、library(caches/preferences)、tmp
Documents:保存文档信息(例如图片,视频等)的路径信息
Library--caches:存放体积比较大的缓存资源
Library--preferences:保存应用的所有偏好设置(NSUsersDefault)
tmp:保存运行时所需的临时数据
3.获取文件目录
获取当前应用程序的沙盒根目录
NSString *rootPath = NSHomeDirectory();
获取Documents文件目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
获取caches文件目录
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"%@",cachePath);
获取preference文件目录
NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"%@",libPath); NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"%@",cachePath)
获取tmp目录 ○
NSString *tmpPath = NSTemporaryDirectory();
4.四种简单对象
NSString/NSArray/NSDictionary/NSData
5.文件处理器(NSFileManager)与文件处理器(NSFileHandle)
NSFileManager
// 在Documents文件夹下,创建一个文件夹(path),在该文件夹下创建一个文件(test.txt),将一个图片对象存入到该文件中,然后在Caches文件夹下创建一个文件夹名为"testDirectroy",将test.txt文件移动到这个文件夹下. NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *Path = [docPath stringByAppendingPathComponent:@"path"]; NSFileManager *filemanager = [NSFileManager defaultManager]; [filemanager createDirectoryAtPath:Path withIntermediateDirectories:YES attributes:nil error:nil]; NSString *testPath = [Path stringByAppendingPathComponent:@"test.txt"]; UIImage *image = [UIImage imageNamed:@"1.jpg"]; NSData *data = UIImageJPEGRepresentation(image, 1.0); [data writeToFile:testPath atomically:YES]; NSString *cachePath =[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSString *dirPath = [cachePath stringByAppendingPathComponent:@"testDirectroy"]; [filemanager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil]; NSLog(@"dirPath is %@",dirPath); //移动 NSString *desPath = [dirPath stringByAppendingPathComponent:@"aa.txt"]; [filemanager moveItemAtPath:testPath toPath:desPath error:nil];
NSFileHandle
对文件内容进行局部修改追加
6.归档与反归档
归档
//创建两个人 Person *bada = [[Person alloc] initWithName:@"bada" age:@"22" gender:@"男"]; Person *qiuxiang = [[Person alloc] initWithName:@"qiuxiang" age:@"18" gender:@"女"]; //获取路径 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; //iOS 中的归档类是NSKeyedArchiver,这个类的作用是将复杂对象转换成NSData对象 //创建一个可变数据对象 NSMutableData *mData = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData]; //归档时要给归档对象添加标记 [archiver encodeObject:bada forKey:@"bada"]; [archiver encodeObject:qiuxiang forKey:@"qiuxiang"]; [archiver finishEncoding];//结束归档,不管还有多少未进行归档的对象,都不会执行归档操作 //将数据写入文件 //创建文件路径 NSString *filePath = [docPath stringByAppendingPathComponent:@"love.png"];//为二进制流文件,什么格式都可以 [mData writeToFile:filePath atomically:YES];
解档
//获取到存放数据的路径 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [docPath stringByAppendingPathComponent:@"love.png"]; //获取NSData对象 NSData *data = [NSData dataWithContentsOfFile:filePath]; //iOS中的解档类是NSKeyedUnarchiver,他的作用是将NSData对象还原成原本的复杂对象 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; //解档 Person *person = [unarchiver decodeObjectForKey:@"bada"]; NSLog(@"%@,%@,%@",person.name,person.age,person.gender);