01-文件管理器
1 #import <Foundation/Foundation.h> 2 #import "User.h" 3 4 int main(int argc, const char * argv[]) 5 { 6 7 @autoreleasepool { 8 9 10 //1.文件管理器 11 NSFileManager * f1 = [NSFileManager defaultManager]; 12 NSLog(@"%p",f1); 13 14 NSFileManager * f2 = [NSFileManager defaultManager]; 15 NSLog(@"%p",f2); 16 17 NSFileManager * f3 = [[NSFileManager alloc] init]; 18 NSLog(@"f3 %p",f3); 19 20 NSFileManager * f4 = [NSFileManager defaultManager]; 21 NSLog(@"%p",f4); 22 23 //在整个程序没有退出之前,只需要创建一次,我们就可以把它写成单利模式 24 //简单的单例模式 25 User * u1 = [User defaultUser]; 26 u1.age = 10; 27 28 29 NSLog(@"u1 %p",u1); 30 31 32 User * u2 = [User defaultUser]; 33 34 NSLog(@"u2 %p age = %d",u2,u2.age); 35 36 User * u3 = [[User alloc] init]; 37 38 NSLog(@"u3 %p",u3); 39 40 //1.组合模式,(聚合模式) 41 //2.单利模式 42 //3.策略模式(封装变化点),block 43 //4.向上抽象 (protocol(协议)) 44 //5.享元设计模式(NSString池原理) 45 } 46 return 0; 47 }
1 #import <Foundation/Foundation.h> 2 3 @interface User : NSObject 4 5 @property (nonatomic,assign)int age; 6 7 + (User *)defaultUser; 8 9 @end
1 #import "User.h" 2 3 @implementation User 4 5 //静态变量,整个程序运行完成的时候才会被释放掉,mian函数运行完成 6 static User * instance = nil; 7 8 + (User *)defaultUser 9 { 10 // return [[self alloc] init]; 11 12 if (instance == nil) 13 { 14 instance = [[self alloc] init]; 15 } 16 return instance; 17 18 } 19 20 @end
02-获取文件属性
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 /* 9 NSString * str = @"www.itcast.cn"; 10 NSError * error; 11 BOOL isWriteSuccess = [str writeToFile:@"/Users/aplle/Desktop/test.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error]; 12 if (isWriteSuccess) 13 { 14 NSLog(@"创建成功"); 15 } 16 else 17 { 18 NSLog(@"error %@",error); 19 } 20 */ 21 22 //1.获取文件管理器 23 NSFileManager * fileManager = [NSFileManager defaultManager]; 24 NSString * path = @"/Users/aplle/Desktop/test.txt"; 25 NSError * error; 26 NSDictionary * dic = [fileManager attributesOfItemAtPath:path error:&error]; 27 28 29 NSLog(@"dic %@",dic); 30 31 if (error == nil) 32 { 33 NSDate * date = [dic objectForKey:NSFileCreationDate]; 34 NSString * size = [dic objectForKey:NSFileSize]; 35 NSLog(@" date = %@, size = %@",date,size); 36 } 37 } 38 return 0; 39 } 03-获取目录下的文件与子目录列表
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 /* 9 //1. 10 NSFileManager * fileManager = [NSFileManager defaultManager]; 11 NSString * path = @"/Users/aplle/Desktop/装机"; 12 NSError * error; 13 14 //只能够火的目标目录下,第一级目录 15 NSArray * array = [fileManager contentsOfDirectoryAtPath:path error:&error]; 16 NSLog(@"array %@",array); 17 */ 18 19 //2 20 NSFileManager * fileManager = [NSFileManager defaultManager]; 21 NSString * path = @"/Users/aplle/Desktop/other"; 22 NSError * error; 23 //能够逐级的获得所有子集的目录 24 NSArray * array = [fileManager subpathsOfDirectoryAtPath:path error:&error]; 25 NSLog(@"array %@",array); 26 27 28 29 30 31 } 32 return 0; 33 }
04-管理目录
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 /* 9 //1创建目录 10 NSFileManager * fileManager = [NSFileManager defaultManager]; 11 NSString * path = @"/Users/aplle/Desktop/myfolder/aaa"; 12 NSError * error; 13 14 //withIntermediateDirectories YES 逐级创建文件夹,NO表示只能够创建一级目录 15 16 BOOL isCreateSuccess = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]; 17 if (isCreateSuccess) 18 { 19 NSLog(@"创建成功"); 20 } 21 else 22 { 23 NSLog(@"error %@",error); 24 } 25 */ 26 27 //2 移动目录 移动就是剪切操作 28 /* 29 NSFileManager * fileManager = [NSFileManager defaultManager]; 30 NSString * path = @"/Users/aplle/Desktop/myfolder"; 31 NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder"; 32 NSError * error ; 33 BOOL isMoveSuccess = [fileManager moveItemAtPath:path toPath:pathTo error:&error]; 34 if (isMoveSuccess) 35 { 36 NSLog(@"移动成功"); 37 } 38 else 39 { 40 NSLog(@"error %@",error); 41 } 42 */ 43 44 //3.删除 45 /* 46 NSFileManager * fileManager = [NSFileManager defaultManager]; 47 NSString * path = @"/Users/aplle/Desktop/newmyfolder"; 48 NSError * error; 49 BOOL isRemoveSuccess = [fileManager removeItemAtPath:path error:&error]; 50 if (isRemoveSuccess) 51 { 52 NSLog(@"删除成功"); 53 } 54 else 55 { 56 NSLog(@"erro %@",error); 57 } 58 */ 59 60 //4.拷贝文件 61 NSFileManager * fileManager = [NSFileManager defaultManager]; 62 NSString * path = @"/Users/aplle/Desktop/myfolder"; 63 NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder"; 64 NSError * error; 65 BOOL isCopySuccess = [fileManager copyItemAtPath:path toPath:pathTo error:&error]; 66 if(isCopySuccess) 67 { 68 NSLog(@"拷贝成功"); 69 } 70 else 71 { 72 NSLog(@"error %@",error); 73 } 74 75 } 76 return 0; 77 }05-管理文件
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 //1.获得文件 9 //mp3.mp4 rmvb mkv pdf doc txt 10 /* 11 NSString * path = @"/Users/aplle/Desktop/屏幕快照 .png"; 12 NSData * data = [NSData dataWithContentsOfFile:path]; 13 NSLog(@"%ld",data.length); 14 15 NSFileManager * fileManager = [NSFileManager defaultManager]; 16 NSString * pathTo = @"/Users/aplle/Desktop/屏幕快照副本.png"; 17 18 BOOL isCreateSuccess = [fileManager createFileAtPath:pathTo contents:data attributes:nil]; 19 20 if (isCreateSuccess) 21 { 22 NSLog(@"创建成功"); 23 } 24 else 25 { 26 NSLog(@"创建失败"); 27 } 28 */ 29 30 /* 31 //2.移动文件 相当于剪切操作 32 NSFileManager * fileManager = [NSFileManager defaultManager]; 33 NSString * path = @"/Users/aplle/Desktop/屏幕快照 .png"; 34 NSString * pathTo = @"/Users/aplle/Desktop/newmyfolder/屏幕屏幕.png"; 35 NSError * error; 36 BOOL ismoveSuccess = [fileManager moveItemAtPath:path toPath:pathTo error:&error]; 37 if (ismoveSuccess) 38 { 39 NSLog(@"移动成功"); 40 } 41 else 42 { 43 NSLog(@"error %@",error); 44 } 45 */ 46 47 NSFileManager * fileManager = [NSFileManager defaultManager]; 48 NSString * path = @"/Users/aplle/Desktop/屏幕快照副本.png"; 49 NSError * error; 50 BOOL isremove = [fileManager removeItemAtPath:path error:&error]; 51 if (isremove) 52 { 53 NSLog(@"删除成功"); 54 } 55 else 56 { 57 NSLog(@"error %@",error); 58 } 59 60 } 61 return 0; 62 }
06-使用NSData处理数据
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 //音频文件,文本文件,视频文件就必须使用NSData数据类 9 /* 10 NSString * str = @"www.itcast.cn"; 11 NSError * error; 12 [str writeToFile:@"/Users/aplle/Desktop/text.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error]; 13 */ 14 /* 15 NSString * path = @"/Users/aplle/Desktop/text.txt"; 16 //把目标文件加载到内存中. 17 //NSData 是不可变长度的Data类型,只能够通过一次加载完成所有的数据 18 NSData * data = [NSData dataWithContentsOfFile:path]; 19 20 NSLog(@"data length %ld",data.length); 21 NSString * pathTo = @"/Users/aplle/Desktop/nextext.txt"; 22 //把加载到内存中的文件,写入到指定的位置 23 BOOL isWriteSuccess = [data writeToFile:pathTo atomically:YES]; 24 25 if (isWriteSuccess) 26 { 27 NSLog(@"写入成功"); 28 } 29 else 30 { 31 NSLog(@"写入失败"); 32 } 33 */ 34 35 //当需要把多个NSData数据拼接成一个数据存储的时候,就要想到使用NSMutableData这个类型 36 NSString * str1 = @"好好学习"; 37 NSString * str2 = @"天天向上"; 38 NSString * str3 = @"今天休息"; 39 40 NSMutableData * muData = [[NSMutableData alloc] init]; 41 42 NSData * d1 = [str1 dataUsingEncoding:NSUTF8StringEncoding]; 43 NSData * d2 = [str2 dataUsingEncoding:NSUTF8StringEncoding]; 44 NSData * d3 = [str3 dataUsingEncoding:NSUTF8StringEncoding]; 45 46 //appendData 能够把nsdata对象加入到 muData对象中 47 [muData appendData:d1]; 48 [muData appendData:d2]; 49 [muData appendData:d3]; 50 51 NSString * path = @"/Users/aplle/Desktop/传智播客校训.txt"; 52 //NSMutableData是继承至NSData的所以可以调用writeToFile 把数据写入到一个指定的目录下 53 BOOL iswriteSucess = [muData writeToFile:path atomically:YES]; 54 if (iswriteSucess) 55 { 56 NSLog(@"创建成功"); 57 } 58 else 59 { 60 NSLog(@"创建失败"); 61 } 62 63 64 65 66 67 } 68 return 0; 69 }
时间: 2024-10-06 21:44:13