標籤:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // 沙箱(SandBox) // Documents(檔案文檔, 使用者主動資料存放區) // Libray(資源, 一般用來存放, 程式員要儲存的一些資料) // ?? // Cache (快取檔案) // Perferences (使用者資訊和一些使用者佈建, NSUserDefaults) // tmp(臨時目錄, 下載的臨時檔案一般放這裡) [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"]; [[NSUserDefaults standardUserDefaults] synchronize]; // 2. 擷取沙箱路徑 // 下面是兩個快捷擷取到目錄的 C 語言的函數 // 根目錄 家目錄 NSHomeDirectory(); NSLog(@"Home------%@", NSHomeDirectory()); // 臨時目錄 tmp 目錄 NSTemporaryDirectory(); NSLog(@"Temporary-----%@", NSTemporaryDirectory()); // C 函數 // 參數1: 搜尋資料夾路徑 NSSearchPathDirectory // 常用: NSDocumentDirectory NSLibraryDirectory NSCachesDirectory // 參數2: 在使用者範圍下搜尋 // 參數3: YES or NO YES代表絕對路徑(基本上用絕對路徑), NO代表相對路徑(~) NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSLog(@"%@", pathArray); [pathArray firstObject]; // NSBundle .app檔案包 NSLog(@"%@", [NSBundle mainBundle]); // 1> 簡單的檔案讀寫 Input Output NSString *hello = @"Hello, I/O"; // 一般拼接路徑時, 使用 stringByAppendingPathComponent 會自動加斜杠 NSString *writePath = [[pathArray firstObject] stringByAppendingPathComponent:@"hello.txt"]; NSError *error = nil; [hello writeToFile:writePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; if (error) { NSLog(@"儲存失敗"); } else { NSLog(@"儲存成功"); } // 2> 讀取路徑對應的文字 NSError *readError = nil; NSString *readString = [NSString stringWithContentsOfFile:writePath encoding:NSUTF8StringEncoding error:&readError]; NSLog(@"%@", readString); // 3> 將 數組 寫入本地檔案 NSArray *array = @[@"黃航", @"韓旭", @"爆花", @"寶寶"]; NSString *arrayPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"]; BOOL isArrayWriteSuccess = [array writeToFile:arrayPath atomically:YES]; if (isArrayWriteSuccess) { NSLog(@"寫入成功"); } else { NSLog(@"寫入失敗"); } // 4> 將 數組 讀取 NSArray *nameArray = [NSArray arrayWithContentsOfFile:arrayPath]; NSLog(@"%@", nameArray); // 5> 將 字典 寫入本地 NSDictionary *dict = @{@"name":@"mafeng", @"age":@"23", @"sex":@"man"}; NSString *dictPath = [[pathArray firstObject] stringByAppendingPathComponent:@"mafeng.plist"]; BOOL isDictWriteSuccess = [dict writeToFile:dictPath atomically:YES]; if (isDictWriteSuccess) { NSLog(@"寫入成功"); } else { NSLog(@"寫入失敗"); } // 6> 將字典讀取出來 NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dictPath]; NSLog(@"%@", dic); // 7> 將Data類型寫入本地 UIImage *image = [UIImage imageNamed:@"user"]; NSString *dataPath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageData"]; NSData *imageData = UIImageJPEGRepresentation(image, 0.1); BOOL isDataWriteSuccess = [imageData writeToFile:dataPath atomically:YES]; NSLog(@"%@", imageData); if (isDataWriteSuccess) { NSLog(@"寫入成功"); } else { NSLog(@"寫入失敗"); } NSData *imageNewData = [NSData dataWithContentsOfFile:dataPath]; UIImage *fileImage = [UIImage imageWithData:imageNewData]; // 2. 複雜物件檔案讀寫, 自訂類型 // 歸檔/反歸檔, 序列化/還原序列化 // 1> 歸檔, 將 對象 儲存到本地 Book *book = [Book new]; book.bookName = @"放棄iOS從我做起"; book.bookType = @"教育"; book.bookPrice = @"988.5"; book.bookAuthor = @"晃晃"; book.bookAddress = @"演變大學"; NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"book.plist"]; BOOL isSuccess = [NSKeyedArchiver archiveRootObject:book toFile:bookPath]; if (isSuccess) { NSLog(@"寫入成功"); } // 2> 反歸檔 Book *huangBook = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath]; NSLog(@"%@", huangBook.bookName); // 如果對象想要實現歸檔和反歸檔 // 1. 對象對應的類需要簽訂 Coding // 2. 實現寫一方法 // 1> initWithCoder 反歸檔用 // 2> encodeWithCoder 歸檔用 // 3. 歸檔時使用 KeyedArchiver // 4. 反歸檔時, 使用 KeyedUnarchiver // 建立一個檔案管理工具 NSFileManager *manager = [NSFileManager defaultManager]; NSString *filePath = [[pathArray firstObject] stringByAppendingPathComponent:@"10101"]; // 建立檔案夾 [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; // 檔案是否存在 BOOL isExists = [manager fileExistsAtPath:filePath]; // 刪除檔案 BOOL isDele = [manager removeItemAtPath:bookPath error:nil]; if (isDele) { NSLog(@"刪除成功"); } else { NSLog(@"刪除失敗"); } if (isExists) { NSLog(@"檔案夾存在"); // 拷貝檔案 NSString *copyPath = [filePath stringByAppendingPathComponent:@"dict.plist"];; BOOL isCopy = [manager copyItemAtPath:dictPath toPath:copyPath error:nil]; if (isCopy) { NSLog(@"拷貝成功"); } else { NSLog(@"拷貝失敗"); } // 移動檔案 NSString *movePath = [filePath stringByAppendingPathComponent:@"mov.plist"];; BOOL isMove = [manager moveItemAtPath:dictPath toPath:movePath error:nil]; if (isMove) { NSLog(@"移動成功"); } else { NSLog(@"移動失敗"); } } else { NSLog(@"檔案夾不存在"); } return YES;}
iOS檔案和檔案夾的建立,刪除,移動, 拷貝,是否存在及單一資料型別的讀寫