1 // 1獲取沙盒路徑 2 NSString *home = NSHomeDirectory(); 3 NSLog(@"%@",home); 4 5 // NSFileManager 單例設計模式 6 NSFileManager *fileManager = [NSFileManager defaultManager]; 7 // NSFileManager *fileManager1 = [NSFileManager defaultManager]; 8 // NSLog(@"fileManager=%@",fileManager); 9 // 1.建立文件 10 // 提供路徑 11 NSString *path = [home stringByAppendingPathComponent:@"file.text"]; 12 NSString *str = @"哈哈呵呵嘻嘻"; 13 NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; 14 // contents 內容 attributes 屬性 15 // 性建立的文件會覆蓋之前的 16 BOOL isGreate = [fileManager createFileAtPath:path contents:data attributes:nil]; 17 18 if (isGreate){ 19 NSLog(@"%@",path); 20 NSLog(@"建立成功"); 21 }else{ 22 23 NSLog(@"建立失敗"); 24 } 25 26 // 2.文件的複製; 27 // 源文件路徑 28 // NSString *path8 = @"/Users/mac/Desktop/VIP第十七次課資料/文件管理/文件管理.xcodeproj"; 29 NSString *path1 = [home stringByAppendingPathComponent:@"file.text"]; 30 // 目標文件的路徑 31 NSString *path2 = [home stringByAppendingPathComponent:@"Pictures/file.text"]; 32 // 若是目標文件已經存在,那麼複製(剪切)會失敗,不會覆蓋原來的文件,若是改變裏面的內容,仍是會失敗 33 BOOL isCopy = [fileManager copyItemAtPath:path1 toPath:path2 error:nil]; 34 if (isCopy) { 35 NSLog(@"%@",path2); 36 NSLog(@"複製成功"); 37 }else{ 38 NSLog(@"複製失敗"); 39 } 40 41 // 3.剪切 42 // [fileManager moveItemAtPath:<#(nonnull NSString *)#> toPath:<#(nonnull NSString *)#> error:]; 43 44 // 4.刪除 45 46 // [fileManager removeItemAtPath:<#(nonnull NSString *)#> error:<#(NSError * _Nullable __autoreleasing * _Nullable)#>]; 47 48 // 5.文件的屬性 49 NSDictionary *dic = [fileManager attributesOfItemAtPath:path2 error:nil]; 50 // NSLog(@"%@",dic); 51 NSNumber *size = [dic objectForKey:@"NSFileSize"]; 52 53 // 6.獲取子目錄 54 NSString *path3 = [home stringByAppendingPathComponent:@"pictures"]; 55 NSArray *pathArray = [fileManager subpathsOfDirectoryAtPath:path3 error:nil]; 56 // NSLog(@"%@",pathArray); 57 // 遍歷子目錄 58 // for (<#type *object#> in pathArray) { 59 // 60 // 61 //// 經過子目錄的路徑獲取該目錄的屬性(放在for in裏面) 62 // 63 // } 64 // 1Mb = 1000k 存儲空間的換算單位:1000, 網絡數據換算是:1024 65 66 return 0;