一、IOS沙盒机制:
iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。
1.1、每个应用程序都有自己的存储空间
1.2、应用程序不能翻过自己的围墙去访问别的存储空间的内容
1.3、应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行。
沙盒一共有4个文件夹
1.Documents:用来长期存放用户文件或文件夹,它是可读写的。
2..APP:存放可执行文件和资源文件,他是只读的。
3.Library: Caches:存放缓存文件,可读写。
4.tmp:存放临时文件,可读写。
得到以上文件目录的方法:
<span style="font-size:14px;">//1.Documents
NSString *home = NSHomeDirectory();
NSString *doc = [home stringByAppendingPathComponent:@"Documents"];
</span>
<span style="font-size:14px;">//2.Caches
NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cache = [array firstObject];
</span>
<span style="font-size:14px;">//3.tmp
NSString *tmp = NSTemporaryDirectory(); </span>
<span style="font-size:14px;">//4.获取工程文件目录
NSString *Path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"type"];
</span>
<span style="font-size:14px;">//5.将一个字符串写入doc文件夹下
NSString *name = @"戴维营教育";
</span><pre name="code" class="objc"><pre name="code" class="objc"><span style="font-size:14px;"> NSString *home = NSHomeDirectory();
NSString *doc = [home stringByAppendingPathComponent:@"Documents"];</span>
NSString *path = [doc stringByAppendingPathComponent:@"test.plist"]; [name writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];<span style="font-size:14px;">//6.Library
NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *library = [paths objectAtIndex:0];</span>
<span style="font-size:14px;">//6.Library
NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *library = [paths objectAtIndex:0];</span>
二、NSFileHandle
Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作
NSFileHandle类中的方法可以对文件进行基本的读写,偏移量的操作。NSFileHandle基本步骤:
1、打开文件,获取一个NSFileHandle对象。
2、对打开NSFileHandle的文件对象行I/O操作
3、关闭文件对象
NSFileHandle常用方法:
+(NSFileHandle*)fileHandleForReadingAtPath:path 打开一个文件用于读入
+(NSFileHandle*)fileHandleForWritingAtPath:path 打开一个文件用于写入
+(NSFileHandle*)fileHandleForUpdatingAtPath:path 打开一个文件用于读写
-(NSData*)availableData 从设备或者通道返回可用数据
-(NSData*)readDataToEndOfFile 读取其余的数据直到文件的末尾(最多UINT_MAX字节)
-(NSData*)readDataOfLength:(NSUInteger)bytes 从文件中读取指定字节的内容
-(void)writeData:data 将data写入文件
-(unsigned long long)offsetInFile 获取当前偏移量
-(void)seekToFileOffset:offset 设置偏移量
-(unsigned long long)seekToEndOfFile 将偏移量定位到文件的末尾
-(void)truncateFileAtOffset:offset 将文件的长度设置为offset字节
-(void)closeFile 关闭文件
方法fileHandleForWritingAtPath和fileHandleForUpdatingAtPath所指定的文件必须是已经存在的,否则
返回nil,另外对于这两个方法中文件的偏移量都是为文件的开始。
方法readDataToEndOfFile每次从文件中读取最多UNIT_MAX字节的数据,这个量定义在
<limits.h>中。
三、归档与解归档
先创建一个Student类,定义3个属性
.h文件
<span style="font-size:14px;">#import <Foundation/Foundation.h>
#import "Dog.h"
@interface Student : NSObject <NSCoding> //挂出协议
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) Dog *dog;
@end
</span>
.m文件
<span style="font-size:14px;">#import "Student.h"
#define kNameKey @"name"
#define kAgeKey @"age"
#define kDogKey @"dog"
@implementation Student
//两个必须实现的方法
- (void)encodeWithCoder:(NSCoder *)aCoder //将属性进行编码
{
[aCoder encodeObject:_name forKey:kNameKey];
[aCoder encodeInteger:_age forKey:kAgeKey];
[aCoder encodeObject:_dog forKey:kDogKey];
}
- (id)initWithCoder:(NSCoder *)aDecoder //将属性进行解码
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:kNameKey];
self.age = [aDecoder decodeIntegerForKey:kAgeKey];
self.dog = [aDecoder decodeObjectForKey:kDogKey];
}
return self;
}
@end
</span>
主程序中进行归档与反归档
<span style="font-size:14px;">- (void)viewDidLoad {
[super viewDidLoad];
NSString *str = @"abc";
[NSKeyedArchiver archiveRootObject:str toFile:@"/Users/apple/Desktop/test.plist"];
NSString *str1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/test.plist"];
NSLog(@"%@", str1);
Student *stu = [[Student alloc] init];
stu.name = @"Zhangsan";
stu.age = 30;
[NSKeyedArchiver archiveRootObject:stu toFile:@"/Users/apple/Desktop/student.plist"];
Student *stu1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/apple/Desktop/student.plist"];
NSLog(@"%p, %p", stu, stu1);
NSLog(@"%@:%d, %@:%d", stu.name, stu.age, stu1.name, stu1.age);
NSData *stuData = [NSKeyedArchiver archivedDataWithRootObject:stu];
[stuData writeToFile:@"/Users/apple/Desktop/stu.plist" atomically:YES];
NSData *stuData2 = [NSData dataWithContentsOfFile:@"/Users/apple/Desktop/stu.plist"];
Student *stu3 = [NSKeyedUnarchiver unarchiveObjectWithData:stuData2];
NSLog(@"%@:%d---%@:%d", stu.name, stu.age, stu3.name, stu3.age);
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:stuData forKey:@"xxxx"];
NSData *stuData3 = [userDefault objectForKey:@"xxxx"];
Student *stu4 = [NSKeyedUnarchiver unarchiveObjectWithData:stuData3];
NSLog(@"%@:%d", stu4.name, stu4.age);
stu4.dog = [[Dog alloc] init];
stu4.dog.name = @"xiaohuang";
[NSKeyedArchiver archiveRootObject:stu4 toFile:@"/Users/apple/Desktop/dog.plist"];
}
</span>