黑马程序员——Foundation框架NSFileManager介绍和用法
来源:互联网 发布:linux tcp backlog 编辑:程序博客网 时间:2024/05/19 13:30
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
第一讲 NSFileManager介绍
概念:
顾名思义, NSFileManager是用来管理文件系统的
它可以用来进行常见的文件\文件夹操作(拷贝、剪切、创建等)
NSFileManager使用了单例模式singleton
使用defaultManager方法可以获得那个单例对象
[NSFileManager defaultManager]
-(BOOL)fileExistsAtPath:(NSString *)path;
path这个文件或文件夹(目录)是否存在
//定义路径NSString *path = @"/User/liwei/Desktop/1.txt";//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//3判断是否存在BOOL isExist = [fileManager fileExistsAtpath:path];if(isExist){ NSLog(@"文件存在");}
-(BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory;
path这个文件或文件夹是否存在, isDirectory代表是否为文件夹
//定义路径NSString *path = @"/User/liwei/Desktop/";//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//定义isDir存储是否是目录(检测结果)BOOL isDir;//3判断是否存在BOOL isExist = [fileManager fileExistsAtpath:path isDirectory:&isDir];if(isExist){ NSLog(@"文件存在"); if(isDir){ NSLog(@"这是一个目录"); }}
-(BOOL)isReadableFileAtPath:(NSString *)path;
path这个文件或文件夹是否可读
//定义路径NSString *path = @"/usr/"; //此目录为系统目录不允许用户写入//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//3判断是否可读BOOL isRead = [fileManager isReadableFileAtPath:Path];if(isRead){ NSLog(@"文件可读");}
-(BOOL)isWritableFileAtPath:(NSString *)path;
path这个文件或文件夹是否可写
//定义路径NSString *path = @"/usr/"; //此目录为系统目录不允许用户写入//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//3判断是否可写BOOL isWrite = [fileManager isWritableFileAtPath:Path];if(isWrite){ NSLog(@"文件或者目录可写");}else{ NSLog(@"文件或者目录不能写入数据");}可写入的目录
//定义路径NSString *path = @"/user/lewei/Desktop/"; //桌面目录允许写入//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//3判断是否可写BOOL isWrite = [fileManager isWritableFileAtPath:Path];if(isWrite){ NSLog(@"文件或者目录可写");}else{ NSLog(@"文件或者目录不能写入数据");}-(BOOL)isDeletableFileAtPath:(NSString *)path;
path这个文件或文件夹是否可删除
//定义路径NSString *path = @"/user/lewei/Desktop/"; //创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//3判断是否可删除BOOL isDelete = [fileManager isDeletableFileAtPath:Path];if(isDelete){ NSLog(@"文件或者目录可以删除");}else{ NSLog(@"文件或者目录不能删除");}
系统目录不能删除
//定义路径NSString *path = @"/usr/"; //系统目录不能删除//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//3判断是否可删除BOOL isDelete = [fileManager isDeletableFileAtPath:Path];if(isDelete){ NSLog(@"文件或者目录可以删除");}else{ NSLog(@"文件或者目录不能删除");}
第二讲 NSFileManager用法深入
NSFileManager文件访问
-(NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error;
获得path这个文件\文件夹的属性
//定义路径NSString *path = @"/usr/"; //创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//3获取属性信息NSDictionary *dict = [fileManager attributesOfItemAtPath:Path error:nil];//查看字典内容NSLog(@"%@",dict);
-(NSArray *)subpathsAtPath:(NSString *)path;
查找给定路径下的所有子路径,返回一个数组, 深度查找,不限于当前层,也会查找package的内容。
//定义路径NSString *path = @"/User/liwei/Desktop/";//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//子路径信息NSArray *pathArr = [fileManager subpathsAtPath:path];NSLog(@"pathArr=%@",apthArr); //注意此处是递归读取
-(NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;
获得path的所有子路径(后代路径),上面两个方法功能一样。
-(NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;
获得path的当前子路径(path下的所有直接子内容,path必须是一个目录)
//定义路径NSString *path = @"/User/liwei/Desktop/";//创建对象NSFileManager *fileManager = [NSFileManager defaultManager];//获取子目录(不再获取后代目录)NSArray *pathArr = [fileManager contentsOfDirectoryAtPath:path error:nil];NSLog(@"pathArr=%@",apthArr);
-(NSData *)contentsAtPath:(NSString *)path;
获得文件内容
第三讲 NSFileManager用法深入(二)
NSFileManager文件操作
-(BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error;
只能创建文件夹(createIntermediates为YES代表自动创建中间的文件夹)
注意如果要创建的目录已经存在,则本次创建失败
//1、定义路径NSString*path = @"/Users/liwei/Desktop/test/subcc/";//2、创建对象NSFileManager*fileManager = [NSFileManagerdefaultManager];//3、创建目录// createDirectoryAtPath 路径// withIntermediateDirectories 是否自动创建路径中的所有文件夹// attributes 文件夹属性// error 错误信息BOOLflag = [fileManager createDirectoryAtPath:path withIntermediateDirectories:NOattributes:nilerror:nil];if(flag) {NSLog(@"目录创建成功!");//写入文件NSString *str =@"测试文件内容";flag = [str writeToFile:[NSString stringWithFormat:@"%@/1.txt",path] atomically:YESencoding:NSUTF8StringEncoding error:nil];if(flag) {NSLog(@"文件写入成功!"); }}-(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;
删除
-(BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
创建文件
(NSData是用来存储二进制字节数据的)
// 把字符串转换为NSData
NSString *s1 =@"hello”;
NSData *data = [s1 dataUsingEncoding:NSUTF8StringEncoding];
1.发送请求给服务器,要求下载某个文件
2.服务器发出响应,返回文件数据
3.手机客户端利用NSData来存放服务器返回的文件数据
4.利用NSFileManager将NSData里面的文件数据写到新的文件中.(createFileAtPath)
- 黑马程序员——Foundation框架NSFileManager介绍和用法
- 黑马程序员——Foundation框架——NSFileManager介绍及使用
- 黑马程序员——Foundation框架——NSFileManager
- 黑马程序员—Foundation框架之NSFileManager、NSNumber与NSDate
- 黑马程序员——OC语言加强---NSFileManager介绍和用法
- 黑马程序员---OC学习笔记之NSFileManager介绍和用法
- 黑马程序员——OC语言加强---Foundation框架介绍
- 黑马程序员——Foundation框架NSNumber和NSValue的介绍和使用
- 黑马程序员——Foundation框架——NSArray和NSMutableArray的介绍及使用
- 黑马程序员——Foundation框架——NSNumber类介绍和使用
- 黑马程序员——Foundation框架——NSValue类介绍和使用
- 黑马程序员——Foundation框架——NSDate、NSCalendar类介绍和使用
- 黑马程序员——Foundation框架NSArray和NSMUtableArray的介绍及使用
- 【黑马程序员】OC-Foundation框架—NSString和NSRange
- 【黑马程序员】OC-Foundation框架—NSArray和NSMutableArray
- 【黑马程序员】OC-Foundation框架—NSDictionary和NSMutableDictionary
- 黑马程序员——Foundation框架
- 黑马程序员——Foundation框架
- iOS 使用模拟器的时候快速切换网络的指令
- mac git 步骤
- 数据结构——向量——向量模板源码
- hibernate 中TIMESTAMP的使用
- js创建对象几种常用方法
- 黑马程序员——Foundation框架NSFileManager介绍和用法
- 第十二章 特质
- RandomAccess接口
- c++数据类型间的转换
- UVALive 5072 Find the Multiples
- 相同格式EXCEL汇总
- 【Leet Code】268. Missing Number---Medium
- ORACLE SQL性能优化三
- 删除集合中重复的数据