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;