數據持久化就是數據保存成文件,存儲到程序中的沙盒中.
沙盒構成
Document 存儲用戶數據,需要備份的信息
Caches 緩存文件, 程序專用的支持文件
Temp 臨時文件
通過代碼查找程序沙盒的相對路徑
NSString *cachespath = cachesPatharray[0];
NSLog(@"%@", cachespath);
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"%@", tempPath);
// 注意 :如果你寫入字典或者數組 那麼數組字典中存儲的數據必須是簡單對象 無法寫入複雜對象
- (void)writeFile
{
// 簡單對象
// 字符串 字典 數組 data...系統寫好的類
// 寫入文件的路徑
// 寫入documents 路徑下寫入xiaoshuo.text
NSArray *documentsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *document = documentsArray[0];
NSString *path = [document stringByAppendingString:@"/xiaoshuo.text"];
NSString *str = @"第一章 在一個月黑風高的早上";
// atomically 如果yes 在你寫入的過程中出現程序崩潰 不影響寫入
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", path);
// 簡單拼接對象寫入步驟
// 1.拼接要寫入的路徑 (注意的路徑一定要拼接對)
// 2. 調用寫方法完事
// 寫入一個數組 shuzu.plist
// 必須給後綴類型 你不給呢 就默認是text格式
NSString *arrPath = [document stringByAppendingPathComponent:@"shuzu.plist"];
NSArray *array = @[@"永樂", @"永飛", @"哈哈"];
// 調用寫入方法
[array writeToFile:arrPath atomically:YES];
NSLog(@"%@", arrPath);
// 寫入一個字典 zidian.plist
NSString *dicPath = [document stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = @{@"name": @"xiaofang"};
[dic writeToFile:dicPath atomically:YES];
NSLog(@"%@", dicPath);
// data的寫入 後綴.da
NSString *dataPath = [document stringByAppendingPathComponent:@"data.da"];
NSString *dataStr = @"你猜我是誰";
NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
// 寫入文件
[data writeToFile:dataPath atomically:YES];
NSLog(@"%@", dataPath);
// 複雜對象
// 自定義的類 比如person
}
// 讀取寫入的文件
- (void)readingFile
{
// 讀字符串
// 獲取路徑
NSArray *documentsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *document = documentsArray[0];
NSString *path = [document stringByAppendingString:@"/xiaoshuo.text"];
// 從路徑中讀取字符串
NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str);
// 讀取數組的文件
NSString *arrPath = [document stringByAppendingPathComponent:@"shuzu.plist"];
// 獲取路徑
NSArray *array = [NSArray arrayWithContentsOfFile:arrPath];
NSLog(@"%@", array);
// 讀取字典
// 獲取路徑
NSString *dicPath = [document stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
NSLog(@"%@", dic);
// 讀取data
NSString *dataPath = [document stringByAppendingPathComponent:@"data.da"];
NSData *data = [NSData dataWithContentsOfFile:dataPath];
// 將data轉化爲字符串
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
.
NSFileManager文件管理者 這個類 是個單例類 用來對文件夾進行操作
- (void)createFile
{
// 需求 在documents下創建一個Download文件夾
NSArray *documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doucumentPath = documentsArr[0];
// 拼接路徑
NSString *downloadPath = [doucumentPath stringByAppendingPathComponent:@"Download"];
NSLog(@"%@", downloadPath);
// 創建文件夾
// 文件管理者 這個類 是個單例類 用來對文件夾進行操作
NSFileManager *manager = [NSFileManager defaultManager];
// withIntermediateDirectories
// 如果yes情況下 要創建的文件 存在的話 可以對其覆蓋
// 反之 文件存在的話 不能對其覆蓋 (創建失敗)
BOOL isCreatFile = [manager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%d", isCreatFile);
}
// 宏定義
#define kDocumentPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
#define kCachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
// 移動文件夾
- (void)moveFile
{
// 獲取原來的路徑
NSString *oldPath = [kDocumentPath stringByAppendingPathComponent:@"Download"];
// 獲取新路徑 libray 下的Caches 文件夾
NSString *newPath = [kCachesPath stringByAppendingPathComponent:@"Download"];
// 創建 文件管理者類的對象(單例對象)
NSFileManager *manager = [NSFileManager defaultManager];
// 移動文件夾
BOOL isMoved = [manager moveItemAtPath:oldPath toPath:newPath error:nil];
NSLog(@"%d", isMoved);
}
// 複製文件夾
- (void)copyFile
{
// libaray 下的Caches 文件夾 download 複製到document文件夾下
NSString *oldPath = [kCachesPath stringByAppendingPathComponent:@"download"];
NSString *newPath = [kDocumentPath stringByAppendingPathComponent:@"downLoad"];
// 創建文件管理對象
NSFileManager *manager = [NSFileManager defaultManager];
// 複製
BOOL isCopy = [manager copyItemAtPath:oldPath toPath:newPath error:nil];
NSLog(@"%d", isCopy);
}
// 刪除文件夾
- (void)deleteFile
{
// 獲取要刪除的路徑
NSString *deletePath = [kDocumentPath stringByAppendingPathComponent:@"Download"];
// 創建文件管理對象
NSFileManager *manager = [NSFileManager defaultManager];
BOOL isDelete = [manager removeItemAtPath:deletePath error:nil];
NSLog(@"%d", isDelete);
}
// 判斷文件夾是否存在
- (void)isExistFile
{
// 獲取 要判斷的路徑
NSString *path = [kCachesPath stringByAppendingPathComponent:@"download"];
// 創建文件管理對象
NSFileManager *manager = [NSFileManager defaultManager];
BOOL isExist = [manager isExecutableFileAtPath:path];
NSLog(@"%d", isExist);
}
// 複雜對象歸檔
- (void)archiver
{
// 初始化對象
JJModel *model = [[JJModel alloc] init];
// 賦值對象
model.name = @"MJJ";
model.age = 49;
// 圖片
// 把一個png格式轉化成data
model.data = UIImagePNGRepresentation([UIImage imageNamed:@"IMG_1873"]);
NSMutableData *data = [NSMutableData data];
// 創建一個歸檔對象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
// 進行歸檔編碼
[archiver encodeObject:model forKey:@"JJModel"];
// 編碼完成
[archiver finishEncoding];
// 實際上歸檔 相當於把編碼完的對象保存data中
// NSLog(@"---------%@", data);
// 把存有複雜對象的文件data 寫入文件中 進行持久化
// 搞路徑
NSString *dataPath = [kDocumentPath stringByAppendingPathComponent:@"JJmodel.da"];
// NSLog(@"=======%@", dataPath);
// 調入寫入方法
[data writeToFile:dataPath atomically:YES];
// 釋放歸檔對象
[archiver release];
- (void)unArchiver
{
// 搞路徑
NSString *path = [kDocumentPath stringByAppendingPathComponent:@"JJModel.da"];
// 獲取剛纔歸檔的data
NSData *data = [NSData dataWithContentsOfFile:path];
// 創建 反歸檔對象
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
// 解碼 返回一個對象 應該是JJModel
// 這個key 一定要和剛纔歸檔的時候的key一致
JJModel *model = [unArchiver decodeObjectForKey:@"JJModel"];
// 反歸檔完成
[unArchiver finishDecoding];
// 釋放反歸檔對象
[unArchiver release];
NSLog(@"%@", model);
UIImage *image = [UIImage imageWithData:model.data];