iOS每個應用程序都有獨立的空間作爲其運行存儲的地方,不同應用之間不允許互相訪問其空間,在iOS8.0之後,逐步開放了部分權限。
一、獲取程序沙盒主路徑的方法
//沙盒的主路徑
NSString* homeStrPath = NSHomeDirectory();
NSLog(@"home--%@",homeStrPath);
//通過search函數得到doctoments文件的路徑
/**
* Description
*
* @param NSDocumentDirectory 所搜尋的文件夾得主路徑
* @param NSUserDomainMask 搜索範圍,此參數是規定搜素區域爲沙盒下的
* @param YES 是否展開,如果是NO的話,是一個~,yes則是完整的路徑
*
* @return 是一個路徑數組的詳細信息
*/
NSArray *documentArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);
NSString *tem=[documentArr objectAtIndex:0];
NSLog(@"searchDocuments -- %@",tem);
二、簡單對象的讀寫操作
iOS中提供的4種類型可以直接進行文件的存取:
NSString(字符串);
NSArray (數組);
NSDictionary(字典);
NSdata (數據);
(以上包括其子類型)。
數組、字典一般寫入爲plist後綴的文件,字符串爲text文件,而數據類型的文件寫入時,不需要加後綴名保存。
三、文件管理器與文件對接器
文件管理器:
//創建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isCreate = [fileManager createDirectoryAtPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"test"]withIntermediateDirectories:YES attributes:nil error:nil];
if (isCreate) {
NSLog(@"goodMan,successful--%@",[SandBoxPaths documentsPath]);
}else{
NSLog(@"default");
}
BOOL isMoved = [fileManager moveItemAtPath:[SandBoxPaths documentsPath] toPath:[[SandBoxPaths cachesPath] stringByAppendingPathComponent:@"test"] error:nil];
if (isMoved) {
NSLog(@"成功");
}
// 複製文件
BOOL isCopy = [fileManager copyItemAtPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"text.txt"]toPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"test/text.txt"] error:nil];
if (isCopy) {
NSLog(@"複製成功");
}else{
NSLog(@"複製失敗");
}
//判斷文件是否相等
BOOL isEaqul = [fileManager contentsEqualAtPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"text.txt"] andPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"text.txt"]];
if (isEaqul) {
NSLog(@"一致");
}else{
NSLog(@"不一致");
}
//刪除文件
[fileManager removeItemAtPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"text.txt"] error:nil];
BOOL isExists = [fileManager fileExistsAtPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"text.txt"]];
if (isExists) {
NSLog(@"存在");
}else{
NSLog(@"不存在");
}
文件對接器:
// 創建一個準備讀取的handle對象
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:[[SandBoxPaths documentsPath]stringByAppendingPathComponent:@"handle.txt"]];
// 完整讀取文件
NSData *endData = [readHandle readDataToEndOfFile];
NSString *string = [[NSString alloc]initWithData:endData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
NSData *lenthData = [readHandle readDataOfLength:8];
NSString *string7 = [[NSString alloc]initWithData:lenthData encoding:NSUTF8StringEncoding];
NSLog(@"特定長度%@",string7);
NSInteger lenth = [[readHandle availableData] length];
NSLog(@"%ld",lenth);
// 創建一個可以寫入的文件對象
NSFileHandle *writeHandle = [NSFileHandle fileHandleForWritingAtPath:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"handle.txt"]];
//改變文件的偏移量
[writeHandle seekToFileOffset:3];
[writeHandle writeData:[@"5678" dataUsingEncoding:NSUTF8StringEncoding]];
//從指定路徑下讀取文件
NSString* result = [NSString stringWithContentsOfFile:[[SandBoxPaths documentsPath] stringByAppendingPathComponent:@"handle.txt"] encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",result);
四、複雜對象的讀寫
<span style="font-size:14px;">//歸檔方法
-(void)archiver{
//創建需要歸檔的對象
UserInfoModel *boy = [[UserInfoModel alloc]init];
boy.name = @"sb";
boy.gender = @"ooo";
boy.age = @"16";
boy.phoneNum = @"11111111111";
boy.headImage = nil;
//創建一個可變的data對象,用來存儲複雜兌現
NSMutableData *data = [NSMutableData data];//用來接收轉換好之後的複雜對對象的容器
//歸檔
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:boy forKey:@"boy"];
//歸檔結束,只有調用了此方法,纔會存儲爲data類型
[archiver finishEncoding];
//將歸檔結束後的數據進行持久化
//構造存儲路徑
//存到cache裏
NSArray *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesPath = [[caches objectAtIndex:0] stringByAppendingPathComponent:@"data"];
//存儲數據
[data writeToFile:cachesPath atomically:YES];
}</span>
<span style="font-size:14px;">//反歸檔,將歸檔好的NSData類型轉化爲複雜對象
-(void)enarchiver{
NSArray *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesPath = [[caches objectAtIndex:0]stringByAppendingPathComponent:@"data"];
NSData *data = [NSData dataWithContentsOfFile:cachesPath];
//反歸檔工具
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
UserInfoModel *boy = [unArchiver decodeObjectForKey:@"boy"];
[unArchiver finishDecoding];
NSLog(@"name -- %@",boy.name);
}</span>
//歸檔遵循的協議
@interface UserInfoModel : NSObject<NSCoding>
//實現協議的代理方法,在這裏是對屬性的編碼
//寫此方法防止奔潰因字典的鍵值不匹配問題
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
//歸檔的協議方法,實際上是對當前類對象所有的屬性進行歸檔,協議方法在我們歸檔的時候會自動調用
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.age forKey:@"age"];
[aCoder encodeObject:self.gender forKey:@"gender"];
[aCoder encodeObject:self.phoneNum forKey:@"phoneNum"];
[aCoder encodeObject:self.headImage forKey:@"headImage"];
NSLog(@"歸檔的方法已經調用");
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeObjectForKey:@"age"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
self.phoneNum = [aDecoder decodeObjectForKey:@"phoneNum"];
self.headImage = [aDecoder decodeObjectForKey:@"headimage"];
}
NSLog(@"解檔調用");
return self;
}