數據持久化,歸檔,反歸檔

    /** 
     * 數據持久化的本質 將數據讀成文件存儲在本地 
     沙盒機制 就是系統針對每個應用程序在本地生成的文件夾 名字隨機生成 對於不同的應用程序 不能訪問其他應用程序沙盒的內容 起到保護作用 
     1)Documents:  用來存儲長久保存的數據 
     2)xxx.app: 應用程序的包 包含應用程序加載所需的所有資源 (readOnly 只讀 不可修改) 平時使用的 NSBundle就是該包 
     3)library : A:(caches) : 本地緩存 存儲想暫時保存的數據 比如 下載的 視頻音頻圖片 都存在該文件下 (自己新建文件 : videos musics images) 
                    : B:(perferences) 存儲用戶的偏好設置 比如程序是否是第一次啓動 
     4)tem : 存儲 還未下載完的 視頻 音頻 當下載完畢 將文件移動到caches 文件夾下 
     */  

-(void)handleWrite:(UIButton *)btn{  
  
    //寫入時 將第一個輸入框的文字 寫入點擊事件  
//    1)獲取存儲的內容  
    UITextField *tf = (UITextField *)[self.view viewWithTag:100];  
    NSString *content = tf.text;  
    UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];  
    NSString *content1 = tf1.text;  
        // 2) 獲取到所要存儲的文件路徑  
//    1.獲取文件夾路徑  
    /** 
     *  用來獲取指定文件夾路徑 
     * 
     *  @param NSDocumentDirectory  指定文件夾 是document 還是library 
     *  @param NSUserDomainMask  設置查找的域 我們自己的文件都是存儲在用戶域的 
     *  @param YES                 是否使用 詳細路徑 
     * 
     *  @return  最初該方法適用於MacOs的而對於電腦系統來說 可能會存儲多個用戶 所以獲取的路徑會有多條 返回值是數組 但在 ios下 只有一個用戶 數組中只有一個原素 
     */  
//    NSString *newFilePath = [self getFilePath];  
//    3) 將內容存儲到指定文件路徑  
//    1.字符串寫入本地文件  
//    NSError *error = nil;  
//    BOOL isSucceed =  [content writeToFile:newFilePath atomically:YES encoding:NSUTF8StringEncoding error:&error];  
//    2. 數組寫入本地文件  
//    NSArray *arr = @[content,content1];  
//    BOOL isScceed = [arr writeToFile:[self getFilePath] atomically:YES];  
//    NSLog(@"%d",isScceed);  
      
    NSDictionary *dic =@{@"tf1" : content,@"tf2" : content1};  
    BOOL isSucceed =[dic writeToFile:[self getFilePath] atomically:YES];  
    NSLog(@"%d",isSucceed);  
      
};  
-(void)handleRead:(UIButton *)btn{  
    //每次寫入都會將內容覆蓋掉 如果需要保留之前的數據 需先將之前的數據拼接在一起 一塊兒寫入  
//    1.字符串從本地文件讀取  
//    NSError *error = nil;  
//    NSString *content = [NSString stringWithContentsOfFile:[self getFilePath] encoding:NSUTF8StringEncoding error:&error];  
    UITextField *tf = (UITextField *)[self.view viewWithTag:100];  
    UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];  
//    tf.text = content;  
//    2.數組從本地文件讀取  
//    NSArray *arr = [NSArray arrayWithContentsOfFile:[self getFilePath]];   
//   tf1.text = arr[1];  
//    tf.text = arr[0];  
//    3.從字典讀取文件  
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:[self getFilePath]];  
    tf.text = dic[@"tf2"];  
    tf1.text = dic[@"tf1"];  
}  
/** 
 *  文件讀寫暫時只支持 數組 字符串 字典 二進制流(nsdata)及他們的子類 
 *寫入文件 : writeToFile:(這是對象調用的方法) 
    讀取文件 : 每個類 自帶的能夠根據文件路徑創建對象的方法 [類名 類WithContentOfFile:] 
 (牢牢謹記) 對於數組字典這種容器來說內部的成員也必須是能夠實現讀寫的八個類之一 
  */  
-(NSString *)getFilePath{  
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];  
    NSString *newFilePath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];  
    return newFilePath;  
} 

// 對於 非以上八大類之一的類 如(自定義的類)則需要使用歸檔 與反歸檔
    -(void)handleArchive:(UIButton *)btn{  
    //    1)獲取輸入框的內容  
        UITextField *tf = (UITextField *)[self.view viewWithTag:100];  
        UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];  
        NSString *content = tf.text;  
        NSString *content1 = tf1.text;  
    //    2)封裝成Person對象  
        Person *per = [[Person alloc]initWithName:content gender:content1 age:18];  
    //    3)創建歸檔對象  
        NSMutableData *data = [NSMutableData data];  
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];  
    //    4) 歸檔  
        [archiver encodeObject:per forKey:@"110"];  
    //    5) 結束歸檔,當結束歸檔後再歸檔無效  
        [archiver finishEncoding];  
        [per release];  
        [archiver release];  
    //    data 寫入文件  
        [data writeToFile:[self getFilePath] atomically:YES];  
        NSLog(@"歸檔");  
    }  
    -(NSString *)getFilePath{  
    //    1.獲得文件夾路徑  
        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];  
        NSString *newFilePath = [documentsPath stringByAppendingPathComponent:@"bb.txt"];  
        NSLog(@"%@",newFilePath);  
        return newFilePath;  
    }  
    -(void)handleDisarchive:(UIButton *)btn{  
    //    1)根據文件路徑初始化  
        NSMutableData *mdate = [NSMutableData dataWithContentsOfFile:[self getFilePath]];  
        NSLog(@"%@",mdate);  
    //    2)創建反歸檔對象  
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:mdate];  
    //    3)反歸檔  
        Person *per = [unarchiver decodeObjectForKey:@"110"];  
        [unarchiver finishDecoding];  
        [unarchiver release];  
        UITextField *tf = (UITextField *)[self.view viewWithTag:100];  
        UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];  
        tf.text = per.gender;  
        tf1.text = per.name;  
    } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章