您的位置:首页 > 职场人生

黑马程序员-ios学习笔记 Foundation 中的 NSFileManager

2015-08-05 01:17 633 查看
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

NSFileManager

一、定义:NSFileManager是用来管理文件系统的;可以用来进行常见的文件\文件夹操作(拷贝、剪贴、创建等)。

 NSFileManager使用了单例模式singleton

使用defaultManager方法可以获得那个单例对象[NSFileManager defaultManager];

NSFileManager *fm=[NSFileManager defaultManager]; //创建文件管理的单例对象


二、判断用法

-(BOOL)fileExistsAtPath:(NSString*)path;—>判断path这个文件或文件夹是否存在

NSFileManager *fm=[NSFileManager defaultManager];      //创建文件管理的单例对象
NSString *filePath=@"/Users/lanlan/Desktop/arr.plist"; //获取文件路径
BOOL isYES=[fm fileExistsAtPath:filePath];             //判断文件是否存在
NSLog(@"%d",isYES);                                    //存在为yes, 不存在为no


-(BOOL)fileExistsAtPath:(NSString*)path isDirectory:(BOOL*)isDirectory

—>判断path这个文件或文件夹是否存在,sDirectory代表是否为文件夹

NSFileManager *fm=[NSFileManager defaultManager];       //创建文件管理的单例对象
NSString *filePath=@"/Users/lanlan/Desktop/arr.plist";  //获取文件路径
BOOL isDir;
[fm fileExistsAtPath:filePath1 isDirectory:&isDir];     //判断是否这个目录是否存在
if (isDir) {
NSLog(@"这是一个目录");
}else{
NSLog(@"这不是一个目录");
}


-(BOOL)isReadableFileAtPath:(NSString*)path;—>判断path这个文件或文件夹是否可读

-(BOOL)isWritableFileAtPath:(NSString*)path;—>判断path这个文件或文件夹是否可写

-(BOOL)isDeletableFileAtPath:(NSString*)path;—>判断path这个文件或文件夹是否可删除

isYES=[fm isReadableFileAtPath:filePath];  //判断文件是否是可读
isYES=[fm isWritableFileAtPath:filePath];  //判断文件是否是可写
isYES=[fm isDeletableFileAtPath:filePath]; //判断文件是否可删除
NSLog(@"%d",isYES);


三、文件访问

-(NSDictionary*)attributesOfItemAtPath:(NSString*)path error:(NSError**)error;

—>获得path这个文件\文件夹的属性

NSDictionary *dict=[fm attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dict);


-(NSArray*)subpathsAtPath:(NSString*)path;

—>查找给定路径下的所有子路径,返回一个数组,深度查找,不限于当前层,也会查找到package内容。

-(NSArray*)subpathsOfDirectoryAtPath:(NSString*)path error:(NSError**)error;

—>获得path的所有子路径(后代路径),上面两个方法功能一样。

NSArray *subPaths=[fm subpathsAtPath:filePath1];             //使用递归的方式获取当前目录及子目录下的文件及文件夹
subPaths=[fm subpathsOfDirectoryAtPath:filePath1 error:nil]; //不是用递归方式,只获取当前目录下子目录信息
NSLog(@"%@",subPaths);


-(NSArray*)contentsOfDirectoryAtPath:(NSString*)path error:(NSError**)error;

—>获得path的当前子路径(path下的所有直接子内容,path必须是一个目录)。

-(NSData*)contentsAtPath:(NSString*)path;—>获得文件内容

四、文件操作

-(BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL) createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error; 

->只能创建文件夹(createIntermediates为YES代表自动创建中间的文件夹) 注意如果要创建的目录已经存在,则本次创建失败 

NSString *creatDirPath=@"/Users/lanlan/Desktop/aaa/cc/love.xml"; //目录
BOOL isYES1=[fm createDirectoryAtPath:creatDirPath withIntermediateDirectories:YES attributes:nil error:nil];  //创建目录
if (isYES1) {
NSLog(@"成功");
}
//创建文件
NSString *str=@"我是一只小小鸟";                                                 //写文件的内容
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];                     //对写入的内容进行转换
BOOL isYES2=[fm createFileAtPath:creatDirPath contents:data attributes:nil];   //创建文件
NSLog(@"%d",isYES2);


- (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; —>删除 

NSString *creatDirPath=@"/Users/lanlan/Desktop/aaa/cc/love.xml"; //目录
NSString *targetPath=@"/Users/lanlan/Desktop/aaa/love.xml";      //目录
[fm copyItemAtPath:creatDirPath toPath:targetPath error:nil];     //拷贝文件
[fm  moveItemAtPath:creatDirPath toPath:targetPath error:nil];    //移动文件
[fm removeItemAtPath:targetPath error:nil];                       //删除文件


------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息