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

黑马程序员---OC学习笔记之NSFileManager介绍和用法

2015-10-08 17:34 621 查看
——Java培训、Android培训、iOS培训、.Net培训——–

一、NSFileManager介绍和用法

1、介绍

1)顾名思义,NSFileManager是用来管理系统文件的。

它可以用来进行常见的文件\文件夹的操作(拷贝、剪切、创建等)

2)NSFileManager使用了单例模式singleton

使用defaultManager可以获取那个单例对象

2、用于判断

//NSFileManager用于判断
NSString *filePath = @"/Users/amos/Desktop/test.txt";
NSString *dirPath = @"/Users/amos/Desktop";
//获取manager单例对象:即程序运行期间只有这一个FileManager对象存在
NSFileManager *manager = [NSFileManager defaultManager];
//1)判断文件是否存在
if ([manager fileExistsAtPath:filePath]) {
NSLog(@"文件%@存在",filePath);
}else{
NSLog(@"文件%@不存在",filePath);
}

//2)判断是否是一个目录
BOOL isDir;
if ([manager fileExistsAtPath:dirPath isDirectory:&isDir]) {
NSLog(@"是一个目录");
}
else{
NSLog(@"不是一个目录");
}

//3)判断文件是否可读
if ([manager isReadableFileAtPath:filePath]) {
NSLog(@"该文件可读");
}else{
NSLog(@"该文件不可读");
}
//4)判断文件是否可写
if ([manager isWritableFileAtPath:filePath]) {
NSLog(@"该文件可写");
}else{
NSLog(@"该文件不可写");
}
//5)判断文件是否可删除.系统目录是不可以删除的
if ([manager isDeletableFileAtPath:filePath]) {
NSLog(@"该文件可删除");
}else{
NSLog(@"该文件不可删除");
}


二、NSFileManager用法深入

1、NSFileManager文件访问

//NSFileManager访问文件
NSString *filePath = @"/Users/amos/Desktop/test.txt";
NSString *dirPath = @"/Users/amos/Desktop";

NSFileManager *fm = [NSFileManager defaultManager];
//1)如何获取文件信息(属性)
NSDictionary *fileAttributes = [fm attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",fileAttributes);
//2)获取指定目录下文件及子目录
//使用递归的方式获取当前目录及子目录下所有的文件及其文件夹
NSArray *arr = [fm subpathsAtPath:dirPath];
//NSLog(@"%@",arr);
//3
//与2)的区别:不是用递归的方式获取的。递归的效率比较低
arr = [fm subpathsOfDirectoryAtPath:dirPath error:nil];
NSLog(@"%@",arr);

//4)获取指定目录下的子目录(不再获取后代路径)
arr = [fm contentsOfDirectoryAtPath:dirPath error:nil];
NSLog(@"%@",arr);


2、NSfileManager文件操作

//NSFileManager操作文件
NSString *filePath = @"/Users/amos/Desktop/test/test2.txt";
NSString *dirPath = @"/Users/amos/Desktop/test";
NSFileManager *fm = [NSFileManager defaultManager];

//1)创建目录
//withIntermediateDirectories YES:如果路径下的目录没有也会一起创建出来 NO:不会创建路径下没有的路径,然后报错
//attributes 属性的字典
//返回值:创建成功返回YES,否则返回NO
BOOL isSuccessed = [fm createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];

if (isSuccessed) {
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}

//2)copy文件
NSString *path2 = @"/Users/amos/Desktop/test3.txt";
isSuccessed = [fm copyItemAtPath:filePath toPath:path2 error:nil];
if (isSuccessed) {
NSLog(@"拷贝成功");
}else{
NSLog(@"拷贝失败");
}

//3)移动文件
isSuccessed = [fm moveItemAtPath:path2 toPath:filePath error:nil];
if (isSuccessed) {
NSLog(@"移动成功");
}else{
NSLog(@"移动失败");
}
//4)删除文件
isSuccessed = [fm removeItemAtPath:filePath error:nil];
if (isSuccessed) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败");
}
//5)创建文件
//contents:<#(NSData *)#> 一个处理二进制数据的类
NSString * content = @"I am Amos,I want to fly in the sky!";
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
//attributes:<#(NSDictionary *)#> 属性字典

isSuccessed = [fm createFileAtPath:filePath contents:data attributes:nil];
if (isSuccessed) {
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: