NSFileManager管理文件常用办法
2015-08-21 17:45
411 查看
// 4.1 NSFileManager管理文件常用办法
// 4.2创建文件
//创建NSFileManager对象
NSFileManager*fileManager = [NSFileManager defaultManager];
NSString*homePath1 = NSHomeDirectory();
NSLog(@"homePath1====%@",homePath1);
NSString*pathMg = [homePath1 stringByAppendingPathComponent:@"liRuYi2.text"];
//创建内容
NSString*text = @"媒介生而平等,但有的媒介比其它媒介更加平等,不是人们不读书了,而是文字阅读已经不再是唯一有价值、有营养的阅读形态了。";
//将字符串转换成NSData类型
NSData*date = [text dataUsingEncoding:NSUTF8StringEncoding];
//写入文件
BOOL success = [fileManager createFileAtPath:pathMg contents:date attributes:nil];
NSLog(@"%d",success);
// 4.3读取文件
// 4.3.1 创建NSFileManager对象
NSFileManager*fileManager2 = [NSFileManager defaultManager];
//4.3.2 根据路径读取文件
NSData*fileDate = [fileManager2 contentsAtPath:pathMg];
//4.3.3 将NSDate转换成NSString
NSString*content = [[NSString alloc] initWithData:fileDate encoding:NSUTF8StringEncoding];
NSLog(@"content=====%@",content);
// 4.4 剪切复制
NSString*homePath3 = NSHomeDirectory();
//源文件路径
NSString*srcPath = [homePath3 stringByAppendingPathComponent:@"liRuYi2.text"];
//目标路径
NSString*toPath = [homePath3 stringByAppendingPathComponent:@"Documents/file.text"];
//4.4.1剪切(移动)文件
//将src路径的文件剪切到toPath路径
BOOL isSuccess = [fileManager2 moveItemAtPath:srcPath toPath:toPath error:nil];
NSLog(@"%d",isSuccess);
//4.4.2 复制文件
//源文件路径
NSString*srcPath2 = [homePath3 stringByAppendingPathComponent:@"file.text"];
//目标路径
NSString*toPath2 = [homePath3 stringByAppendingPathComponent:@"Library/good.text"];
BOOL success2 = [fileManager2 copyItemAtPath:srcPath2 toPath:toPath2 error:nil];
NSLog(@"%d",success2);
// 4.5 删除文件
//判断文件是否存在
NSString*deletePath = [homePath3 stringByAppendingPathComponent:@"liRuYi2.text"];
BOOL isExist = [fileManager2 fileExistsAtPath:deletePath];
if (isExist) {
//删除文件
BOOL seccessDelete = [fileManager2 removeItemAtPath:deletePath error:nil];
if (seccessDelete) {
NSLog(@"remove success");
UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:nil message:@"remove success" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
}
// 4.6获取文件大小
//获取文件的属性字典
NSDictionary*fileDic = [fileManager2 attributesOfItemAtPath:toPath2 error:nil];
//获取文件大小
NSNumber*fileSize = [fileDic objectForKey:NSFileSize];
NSLog(@"fileSize=======%@",fileSize);
// 5 写文件
// 数组 字典 字符串 NSData都是容纳数据的,他们都有一个writeToFile的方法
//5.1数组写入文件
NSString *str1 = @"明月几时有";
NSString *str2 = @"把酒问青天";
NSArray *array = [NSArray arrayWithObjects:str1,str2, nil];
NSString*path10 = [homePath stringByAppendingPathComponent:@"hehe.text"];
//将数组中的数据写入文件
[array writeToFile:path10 atomically:YES];
NSString*path11 = [homePath stringByAppendingPathComponent:@"hehe.plist"];
[array writeToFile:path11 atomically:YES];
// 5.2 属性列表文件
// 数组只能写入NSNumber NSString NSDate NSData NSArray NSDictionary
// 数组,字典写入的文件叫属性列表(plist),可以用xcode打开编辑
// 6 读文件
// 数组 字典 字符串 NSData 同样的可以将文件数据读入程序
// 6.1数组读文件
//通过alloc创建,并读入文件数据
NSArray*array1 = [[NSArray alloc] initWithContentsOfFile:path10];
NSLog(@"array1===========%@",array1);
//通过类方法创建,并读入文件数据
NSArray*arry2 = [NSArray arrayWithContentsOfFile:path11];
NSLog(@"arry2 ===== %@",arry2);
// 补充 创建文件夹
//[[NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask, YES)是一个数组,作用是获取路径
NSString*createDir = [[NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test/test/test"];
//创建文件夹 createIntermediates该参数bool类型 是否创建文件夹路径中没有的文件夹目录
[fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
// 4.2创建文件
//创建NSFileManager对象
NSFileManager*fileManager = [NSFileManager defaultManager];
NSString*homePath1 = NSHomeDirectory();
NSLog(@"homePath1====%@",homePath1);
NSString*pathMg = [homePath1 stringByAppendingPathComponent:@"liRuYi2.text"];
//创建内容
NSString*text = @"媒介生而平等,但有的媒介比其它媒介更加平等,不是人们不读书了,而是文字阅读已经不再是唯一有价值、有营养的阅读形态了。";
//将字符串转换成NSData类型
NSData*date = [text dataUsingEncoding:NSUTF8StringEncoding];
//写入文件
BOOL success = [fileManager createFileAtPath:pathMg contents:date attributes:nil];
NSLog(@"%d",success);
// 4.3读取文件
// 4.3.1 创建NSFileManager对象
NSFileManager*fileManager2 = [NSFileManager defaultManager];
//4.3.2 根据路径读取文件
NSData*fileDate = [fileManager2 contentsAtPath:pathMg];
//4.3.3 将NSDate转换成NSString
NSString*content = [[NSString alloc] initWithData:fileDate encoding:NSUTF8StringEncoding];
NSLog(@"content=====%@",content);
// 4.4 剪切复制
NSString*homePath3 = NSHomeDirectory();
//源文件路径
NSString*srcPath = [homePath3 stringByAppendingPathComponent:@"liRuYi2.text"];
//目标路径
NSString*toPath = [homePath3 stringByAppendingPathComponent:@"Documents/file.text"];
//4.4.1剪切(移动)文件
//将src路径的文件剪切到toPath路径
BOOL isSuccess = [fileManager2 moveItemAtPath:srcPath toPath:toPath error:nil];
NSLog(@"%d",isSuccess);
//4.4.2 复制文件
//源文件路径
NSString*srcPath2 = [homePath3 stringByAppendingPathComponent:@"file.text"];
//目标路径
NSString*toPath2 = [homePath3 stringByAppendingPathComponent:@"Library/good.text"];
BOOL success2 = [fileManager2 copyItemAtPath:srcPath2 toPath:toPath2 error:nil];
NSLog(@"%d",success2);
// 4.5 删除文件
//判断文件是否存在
NSString*deletePath = [homePath3 stringByAppendingPathComponent:@"liRuYi2.text"];
BOOL isExist = [fileManager2 fileExistsAtPath:deletePath];
if (isExist) {
//删除文件
BOOL seccessDelete = [fileManager2 removeItemAtPath:deletePath error:nil];
if (seccessDelete) {
NSLog(@"remove success");
UIAlertView*alertView = [[UIAlertView alloc] initWithTitle:nil message:@"remove success" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
}
// 4.6获取文件大小
//获取文件的属性字典
NSDictionary*fileDic = [fileManager2 attributesOfItemAtPath:toPath2 error:nil];
//获取文件大小
NSNumber*fileSize = [fileDic objectForKey:NSFileSize];
NSLog(@"fileSize=======%@",fileSize);
// 5 写文件
// 数组 字典 字符串 NSData都是容纳数据的,他们都有一个writeToFile的方法
//5.1数组写入文件
NSString *str1 = @"明月几时有";
NSString *str2 = @"把酒问青天";
NSArray *array = [NSArray arrayWithObjects:str1,str2, nil];
NSString*path10 = [homePath stringByAppendingPathComponent:@"hehe.text"];
//将数组中的数据写入文件
[array writeToFile:path10 atomically:YES];
NSString*path11 = [homePath stringByAppendingPathComponent:@"hehe.plist"];
[array writeToFile:path11 atomically:YES];
// 5.2 属性列表文件
// 数组只能写入NSNumber NSString NSDate NSData NSArray NSDictionary
// 数组,字典写入的文件叫属性列表(plist),可以用xcode打开编辑
// 6 读文件
// 数组 字典 字符串 NSData 同样的可以将文件数据读入程序
// 6.1数组读文件
//通过alloc创建,并读入文件数据
NSArray*array1 = [[NSArray alloc] initWithContentsOfFile:path10];
NSLog(@"array1===========%@",array1);
//通过类方法创建,并读入文件数据
NSArray*arry2 = [NSArray arrayWithContentsOfFile:path11];
NSLog(@"arry2 ===== %@",arry2);
// 补充 创建文件夹
//[[NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask, YES)是一个数组,作用是获取路径
NSString*createDir = [[NSSearchPathForDirectoriesInDomains(NSDemoApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test/test/test"];
//创建文件夹 createIntermediates该参数bool类型 是否创建文件夹路径中没有的文件夹目录
[fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
相关文章推荐
- 同步代码块,同步方法,同步静态方法,同步非静态方法的区别
- 由于协议错误,回话将被中断。请重新连接到远程计算机。
- ios UI开发相关转载链接汇总
- CF 219 D Choosing Capital for Treeland(树形dp)
- php:内存回收机制
- 新安装的wampserver怎么使用本机已有的mysql作为数据库
- c查找算法——线性索引查找
- 目前为止存在的这些问题
- python 打开文件,写入文件,关闭文件,读文件,清空文件内容
- Java基础——成员变量、局部变量和静态变量的区别
- 二进制-十进制-十六进制-含义解释
- JQuery插件开发之如何实时显示被隐藏的输入框密码
- SSH整合之架构的历史---用户注册
- [hive]Can't create/write to file '/tmp/#sql_2290_0.MYI' (Errcode: 13)
- 排序算法性能及选择总结
- Dmidecode命令详解
- 推荐一些好的网站,非常实用(业界推荐)
- jmeter上传文件搞了一天,才搞定,没高人帮忙效率就是低,赶紧记下来,以备后用
- 析构函数中的virtual是否必要?
- UI_iOS应用状态栏样式设置_及导航栏(NavigationBar)透明