iOS开发12-iOS NSFileManager的使用
2015-10-25 16:13
471 查看
iOS开发12-iOS NSFileManager的使用
代码下载(Xcode7.0.1)有问题请联系博主,邮箱:nathanlee1987@aliyun.com
1、对文件夹的操作(创建、移动、删除、重命名、判断是否存在)
//NSFileManager //1.创建文件夹 //2.移动 //3.重命名(实质是移动) //4.删除 //5.判断是否存在某个文件夹 //获取Document路径 NSString * Document=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSFileManager *m=[NSFileManager defaultManager];//单例 NSString *filePath1=[Document stringByAppendingPathComponent:@"H1/h1"]; NSString *filePath2=[Document stringByAppendingPathComponent:@"H2"]; //1.创建文件夹 [m createDirectoryAtPath:filePath1 withIntermediateDirectories:YES attributes:nil error:nil]; [m createDirectoryAtPath:filePath2 withIntermediateDirectories:YES attributes:nil error:nil]; //withIntermediateDirectories @"H1/h1" 带中间路径,YES 的话可以创建。 NSLog(@"Document=%@",Document); //2.移动 NSString *filePathH1=[Document stringByAppendingPathComponent:@"/H1/h1"]; NSString *filePatH2=[Document stringByAppendingPathComponent:@"H2/h1"]; //移动过去后叫什么名字 // /H1/h1移动到--->H2/h1 [m moveItemAtPath:filePathH1 toPath:filePatH2 error:nil]; NSLog(@"filePathH1=%@",filePathH1); NSLog(@"filePatH2=%@",filePatH2); //3.重命名(实质是移动) [m moveItemAtPath:[Document stringByAppendingPathComponent:@"/H2/h1"] toPath:[Document stringByAppendingPathComponent:@"/H2/hnew"] error:nil]; //4.删除 [m removeItemAtPath:[Document stringByAppendingPathComponent:@"/H2/hnew"] error:nil]; //5.判断是否存在 BOOL isE=[m fileExistsAtPath:[Document stringByAppendingPathComponent:@"/H1"]]; NSLog(@"%d",isE);
运行结果:
/* 2015-10-25 15:53:00.646 NSFileManager[1106:92995] Document=/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/868CD8A8-2AAA-49C4-851C-D7188BD4B0CA/Documents 2015-10-25 15:53:00.647 NSFileManager[1106:92995] filePathH1=/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/868CD8A8-2AAA-49C4-851C-D7188BD4B0CA/Documents/H1/h1 2015-10-25 15:53:00.647 NSFileManager[1106:92995] filePatH2=/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/868CD8A8-2AAA-49C4-851C-D7188BD4B0CA/Documents/H2/h1 2015-10-25 15:53:00.647 NSFileManager[1106:92995] isE=1 */
2、使用NSFileManager取得目录下所有文件(遍历文件夹下的文件)
//遍历文件夹中所有的文件 - (NSMutableArray*) getAllFilesAtPath:(NSString*) dirString { NSMutableArray* array = [NSMutableArray array]; NSFileManager* m = [NSFileManager defaultManager]; NSArray* tempArr = [m contentsOfDirectoryAtPath:dirString error:nil]; for (NSString* fileName in tempArr) { BOOL flag = YES; NSString* fullPath = [dirString stringByAppendingPathComponent:fileName]; if ([m fileExistsAtPath:fullPath isDirectory:&flag]) { if (!flag)//如果不是文件夹,就添加到数组中 [array addObject:fullPath]; } } return array;//返回数组 }
3、使用NSFileManager取得目录下所有子文件夹(遍历文件夹下的子文件夹)
//遍历文件夹中所有的子文件夹 - (NSMutableArray*) getAllFoldersAtPath:(NSString*) dirString { NSMutableArray* array = [NSMutableArray array]; NSFileManager* m = [NSFileManager defaultManager]; NSArray* tempArr = [m contentsOfDirectoryAtPath:dirString error:nil]; for (NSString* fileName in tempArr) { BOOL flag = YES; NSString* fullPath = [dirString stringByAppendingPathComponent:fileName]; if ([m fileExistsAtPath:fullPath isDirectory:&flag]) { if (flag)//如果是文件夹,就添加到数组中 [array addObject:fullPath]; } } return array;//返回数组 }
4、判断文件是否存在,不存在则创建文件
// 判断文件是否存在,不存在则创建文件 - (void)createFileIfNotExist:(NSString*)dirString { // 判断文件是否存在,不存在则创建文件 NSFileManager *m = [NSFileManager defaultManager]; BOOL isDirExist = [m fileExistsAtPath:dirString]; //如果此文件不存在,那么就创建这个文件 if(!isDirExist) { NSLog(@"此文件不存在,开始创建这个文件"); BOOL createFile = [m createFileAtPath:dirString contents:nil attributes:nil]; if(!createFile) NSLog(@"Create File Failed."); //打印出路径 NSLog(@"%@",dirString); } }
5、判断文件夹是否存在,不存在则创建文件夹
// 判断文件夹是否存在,不存在则创建对应文件夹 - (void)createFolderIfNotExist:(NSString*)dirString { // 判断文件夹是否存在,不存在则创建对应文件夹 NSFileManager *m = [NSFileManager defaultManager]; BOOL isDir = FALSE;//判断是否是文件夹 BOOL isDirExist = [m fileExistsAtPath:dirString isDirectory:&isDir]; //如果此文件夹不存在,那么就创建这个文件夹 if(!(isDirExist && isDir)) { NSLog(@"此文件夹不存在,开始创建这个文件夹"); BOOL createDir = [m createDirectoryAtPath:dirString withIntermediateDirectories:YES attributes:nil error:nil]; if(!createDir) NSLog(@"Create Directory Failed."); //打印出路径 NSLog(@"%@",dirString); } }
6、方法的调用和运行结果
//获取Document路径 NSString * Document=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSFileManager *m=[NSFileManager defaultManager];//单例 //创建字符串 NSString *s1=@"hello world."; //获取路径 NSString *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"documents=%@",documents); //拼接文件名 NSString *filePaths1=[documents stringByAppendingPathComponent:@"hello.txt"]; //写入 [s1 writeToFile:filePaths1 atomically:YES encoding:NSUTF8StringEncoding error:nil]; if ([m fileExistsAtPath:filePaths1]) NSLog(@"文件存在"); else NSLog(@"文件不存在"); BOOL isFolder=YES;//接收是否是文件夹的布尔变量 if ([m fileExistsAtPath:filePaths1 isDirectory:&isFolder]) { if (!isFolder) NSLog(@"文件存在,并且不是文件夹"); } //将 hello.txt 文件拷贝出一个新的文件 newfile.txt //新文件路径和新名字 NSString *filePathsnew=[documents stringByAppendingPathComponent:@"newhello.txt"]; if ([m copyItemAtPath:filePaths1 toPath:filePathsnew error:nil]) { NSLog(@"newhello.txt复制成功"); }; //判断两个文件内容是否相等 if ([m contentsEqualAtPath:filePaths1 andPath:filePathsnew]) NSLog(@"newhello.txt和hello.txt文件内容相等"); //创建文件夹 NSString * newFolder=[Document stringByAppendingPathComponent:@"/H2/newFolder"]; [self createFolderIfNotExist:newFolder]; //创建文件 NSString * newFile2=[Document stringByAppendingPathComponent:@"/H2/newFolder/new.text"]; [self createFileIfNotExist:newFile2]; //遍历document下所有文件 NSLog(@"document下所有文件=%@",[self getAllFilesAtPath:Document]); //遍历document下所有文件夹 NSLog(@"document下所有文件夹=%@",[self getAllFoldersAtPath:Document]);
程序运行结果
//运行结果 /* 2015-10-25 16:06:03.107 NSFileManager[1129:97949] documents=/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/BEE3B871-2EA8-4A2F-9442-D78DD62E2757/Documents 2015-10-25 16:06:03.110 NSFileManager[1129:97949] 文件存在 2015-10-25 16:06:03.110 NSFileManager[1129:97949] 文件存在,并且不是文件夹 2015-10-25 16:06:03.113 NSFileManager[1129:97949] newhello.txt和hello.txt文件内容相等 2015-10-25 16:06:03.114 NSFileManager[1129:97949] document下所有文件=( "/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/BEE3B871-2EA8-4A2F-9442-D78DD62E2757/Documents/.DS_Store", "/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/BEE3B871-2EA8-4A2F-9442-D78DD62E2757/Documents/hello.txt", "/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/BEE3B871-2EA8-4A2F-9442-D78DD62E2757/Documents/newhello.txt" ) 2015-10-25 16:06:03.114 NSFileManager[1129:97949] document下所有文件夹=( "/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/BEE3B871-2EA8-4A2F-9442-D78DD62E2757/Documents/H1", "/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/BEE3B871-2EA8-4A2F-9442-D78DD62E2757/Documents/H2" ) */
7、使用NSFileManager管理NSURLSession下载的图片
// 使用NSFileManager管理NSURLSession下载的图片 NSURLSession *session=[NSURLSession sharedSession]; NSURL *url=[NSURL URLWithString:@"http://japan.xinhuanet.com/2012-02/07/131395002_31n.jpg"]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; NSURLSessionDownloadTask *task=[session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { //location下载完放到的地方 NSLog(@"location=%@",location); //获取Caches路径 NSString * cachesPath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"获取Caches路径=%@",cachesPath); //url NSURL *cachesURL=[NSURL fileURLWithPath:cachesPath]; //获取原名 NSURL *fileURl= [cachesURL URLByAppendingPathComponent:[[response URL] lastPathComponent]]; //改名 NSURL *fileURlnewName= [cachesURL URLByAppendingPathComponent:@"newName.jpg"]; //copy NSFileManager *m=[NSFileManager defaultManager]; //从下载的地方拷贝到目的路径 //不改名 [m copyItemAtURL:location toURL:fileURl error:nil]; //改名 [m copyItemAtURL:location toURL:fileURlnewName error:nil]; NSLog(@"不改名图片路径=%@",fileURl); NSLog(@"改名图片路径=%@",fileURlnewName); }]; [task resume];
运行结果:
/* 2015-10-25 16:27:19.978 NSFileManager[1214:111966] location=file:///Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/A42F72ED-8BD9-4E1A-9E31-89216E7AE44B/tmp/CFNetworkDownload_PI4wzk.tmp 2015-10-25 16:27:19.979 NSFileManager[1214:111966] 获取Caches路径=/Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/A42F72ED-8BD9-4E1A-9E31-89216E7AE44B/Library/Caches 2015-10-25 16:27:19.981 NSFileManager[1214:111966] 不改名图片路径=file:///Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/A42F72ED-8BD9-4E1A-9E31-89216E7AE44B/Library/Caches/131395002_31n.jpg 2015-10-25 16:27:19.981 NSFileManager[1214:111966] 改名图片路径=file:///Users/lining/Library/Developer/CoreSimulator/Devices/4BE9A476-F866-47D0-AA53-F4C3D57452F2/data/Containers/Data/Application/A42F72ED-8BD9-4E1A-9E31-89216E7AE44B/Library/Caches/newName.jpg */
代码下载(Xcode7.0.1)
有问题请联系博主,邮箱:nathanlee1987@aliyun.com
著作权声明:本文由http://my.csdn.net/Nathan1987_原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
相关文章推荐
- iOS 锁屏后暂停歌曲的方法设置
- 在ios开发中有多少常用的加密解密方式(备用)
- iOS:自定义工具栏、导航栏、标签栏
- iOS 百度地图 小的特点demo
- iOS 新浪微博-5.0 首页微博列表
- 转:iOS-生成Bundle包-引入bundle-使用bundle
- iOS开发-Certificates、Identifiers和Profiles详解
- iOS巅峰之键盘回收方法
- IOS的属性和实例变量
- (二期)IOS调试技巧
- 在iOS7下 使用@import代替#import
- ios NSString拼接方法总结
- (iOS)手动内存管理01
- 类簇在iOS开发中的应用
- 自学 iOS 开发的一些经验
- iOS开发如何提高
- iOS开发语言
- iOS程序崩溃 NaN: [nan 40]'
- iOS开发日记36-iOS中的依赖注入
- 作IFRAME于iOS您的设备上支持滚动