iOS文件和文件夾的創建,刪除,移動, 拷貝,是否存在及簡單數據類型的讀寫


- (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;
} 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM