文件管理

 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;

猜你喜欢

转载自www.cnblogs.com/lanmaokomi/p/8926816.html