oc-Foundation_06_NSFileManage
来源:互联网 发布:制图软件手机 编辑:程序博客网 时间:2024/05/16 08:36
- Foundation_06_NSFileManager
- 一NSFileManager介绍
- 二NSFileManager基础
- 1 判断操作
- 11 基本方法介绍
- 12 代码示例
- 13 结果
- 2 文件的属性子目录等
- 21 基本方法介绍
- 22 代码示例
- 3 文件的操作
- 31 基本方法介绍
- 32 代码示例
- 33 结果
- 1 判断操作
Foundation_06_NSFileManager
一、NSFileManager介绍
什么是NSFileManager??
顾名思义,就是用来管理文件系统的。它可以用来进行常见的文件/文件夹操作(拷贝、剪切、创建等)
NSFileManager使用了单例模式singleton
使用defaultManager方法获得那个单例对象
[NSFileManager defaultManager]
二、NSFileManager基础
2.1 判断操作
2.1.1 基本方法介绍
-(BOOL)fileExistsAtPath:(NSString*) 文件或文件夹(目录)是否存在-(BOOL)fileExistsAtPath:(NSString*)isDirectory:(BOOL) 文件或文件夹(目录)是否是目录-(BOOL)isReadableFileAtPath:(NSString*) 文件或文件夹(目录)是否可读-(BOOL)isWritableFileAtPath:(NSString*) 文件或文件夹(目录)是否可写-(BOOL)isDeletableFileAtPath:(NSString*) 文件或文件夹(目录)是否可删除
2.1.2 代码示例
void test(){ NSString *path = @"/Users/qiwenming/Desktop"; NSFileManager *fm = [NSFileManager defaultManager];// 1.文件是否存在 if([fm fileExistsAtPath:path]){ NSLog(@"文件存在:%@",path); }// 2.目录?? if ([fm fileExistsAtPath:path isDirectory:NO]) { NSLog(@"文件是目录:%@",path); }// 3.可读?? if ([fm isReadableFileAtPath:path]) { NSLog(@"文件是可读:%@",path); }// 4.可写?? if ([fm isWritableFileAtPath:path]) { NSLog(@"文件是可写:%@",path); }// 5.删除?? if ([fm isDeletableFileAtPath:path]) { NSLog(@"文件是删除:%@",path); }}
2.1.3 结果
2015-08-17 00:55:49.804 08_Foundation05_NSFileManager[1659:105369] 文件存在:/Users/qiwenming/Desktop2015-08-17 00:55:49.814 08_Foundation05_NSFileManager[1659:105369] 文件是目录:/Users/qiwenming/Desktop2015-08-17 00:55:49.814 08_Foundation05_NSFileManager[1659:105369] 文件是可读:/Users/qiwenming/Desktop2015-08-17 00:55:49.815 08_Foundation05_NSFileManager[1659:105369] 文件是可写:/Users/qiwenming/Desktop2015-08-17 00:55:49.819 08_Foundation05_NSFileManager[1659:105369] 文件是删除:/Users/qiwenming/Desktop
2.2 文件的属性子目录等
2.2.1 基本方法介绍
-(NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error 文件信息(属性)-(NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error 目录下的文件和子文件-(NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error目录下的直接子文件
2.2.2 代码示例
void test2(){ NSString *path = @"/Users/qiwenming/360云盘/IOS学习"; NSFileManager *fm = [NSFileManager defaultManager]; //1.文件信息(属性) NSDictionary *infoDic = [fm attributesOfItemAtPath:path error:nil]; NSLog(@"%@",infoDic); //2.目录下的文件和子文件 //递归 NSArray *fileArr = [fm subpathsAtPath:path]; NSLog(@"%@",fileArr); //不是递归 推荐这种 NSArray *fileArr2 = [fm subpathsOfDirectoryAtPath:path error:nil]; NSLog(@"%@",fileArr2); //3.目录下的直接子文件 NSArray *fileArr3 = [fm contentsOfDirectoryAtPath:path error:nil]; NSLog(@"%@",fileArr3);}
2.3 文件的操作
2.3.1 基本方法介绍
-(BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error 创建目录 -(BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr 创建文件 -(BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error 复制文件 -(BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error 移动文件 -(BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error 删除文件
2.3.2 代码示例
void test3(){ NSFileManager *fm = [NSFileManager defaultManager]; NSString *dicPath = @"/users/qiwenming/desktop/xm"; NSString *filePath = @"/users/qiwenming/desktop/xm/xmm.txt"; NSString *filePath2 = @"/users/qiwenming/desktop/xmmxxx.txt"; NSString *filePath3 = @"/users/qiwenming/desktop/xmmx.txt"; //创建目录 BOOL isDicOK = [fm createDirectoryAtPath:dicPath withIntermediateDirectories:YES attributes:nil error:nil]; if(isDicOK){ NSLog(@"目录创建成功"); }else{ NSLog(@"目录创建失败"); } //创建文件 BOOL isFileOK = [fm createFileAtPath:filePath contents:@"my name is xiaoming!!!" attributes:nil]; if(isFileOK){ NSLog(@"文件创建成功"); }else{ NSLog(@"文件创建失败"); } //copy文件 BOOL isCopyOk = [fm copyItemAtPath:filePath toPath:filePath2 error:nil]; if(isCopyOk){ NSLog(@"文件copy成功"); }else{ NSLog(@"文件copy失败"); } //移动文件 BOOL isMoveOk = [fm moveItemAtPath:filePath toPath:filePath3 error:nil]; if(isMoveOk){ NSLog(@"文件移动成功"); }else{ NSLog(@"文件移动失败"); } //删除文件 BOOL isRemoveOk = [fm removeItemAtPath:filePath2 error:nil]; if(isRemoveOk){ NSLog(@"文件删除成功"); }else{ NSLog(@"文件删除失败"); }}
2.3.3 结果
2015-08-17 22:42:19.353 08_Foundation05_NSFileManager[2235:130146] 目录创建成功2015-08-17 22:42:19.356 08_Foundation05_NSFileManager[2235:130146] 文件创建成功2015-08-17 22:42:19.357 08_Foundation05_NSFileManager[2235:130146] 文件copy成功2015-08-17 22:42:19.358 08_Foundation05_NSFileManager[2235:130146] 文件移动失败2015-08-17 22:42:19.358 08_Foundation05_NSFileManager[2235:130146] 文件删除成功
0 0
- oc-Foundation_06_NSFileManage
- oc
- oc
- OC
- OC
- OC
- oc
- oc
- oc
- OC
- OC
- oc
- oc
- OC
- OC
- OC
- oc
- oc
- QT多线程
- 微信小程序常见FAQ(17.1.2-17.1.8)
- asp.net常用的命名空间及含义
- Django 官方文档write your first Django app --4
- fragment
- oc-Foundation_06_NSFileManage
- 全局设置Gradle
- Android 系统简单播放器
- 研究生周报(第六次) 2016/11/12-2016/11/18
- Java IO
- Android入门——数据存储之SQLite存储读取图片
- 动态规划的几个帖子
- JSONObject 解析json用法
- 计算机组成课程设计之一——微程序控制的运算器设计