文件管理NSFileManager
//NSFileManager
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",NSHomeDirectory());
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [docPath stringByAppendingPathComponent:@"firstFM"];
NSFileManager *fm = [NSFileManager defaultManager];
//一.创建
//1.创建文件夹
if (![fm fileExistsAtPath:filePath]) {
NSError *error = nil;
BOOL isSuc = [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
if (!isSuc) {
NSLog(@"fail:%@",error.localizedDescription);
}
}else{
NSLog(@"file existed");
}
//2.创建文件
NSString *txtPath = [filePath stringByAppendingPathComponent:@"firstTxt.txt"];
NSString *test = @"hello world";
NSData *writeData= [test dataUsingEncoding:NSUTF8StringEncoding];
if (![fm fileExistsAtPath:txtPath]) {
BOOL isSuc = [fm createFileAtPath:txtPath contents:writeData attributes:nil];
if (!isSuc) {
NSLog(@"fail");
}
}else{
NSLog(@"txt existed");
}
//1.
// [self scanDirectoryOrFile:filePath];
//2.
NSString *sourcePath = txtPath;
NSString *newPath = [docPath stringByAppendingPathComponent:@"firstTxt.txt"];
// [self moveDiectoryOrFile:sourcePath toNewPath:newPath];
// [self deleteDirectoryOrFile:[docPath stringByAppendingPathComponent:@"rrr"]];
[self attributeForDirectoryOrFile:newPath];
}
//二.操作
//1.遍历文件夹(目录)
- (void)scanDirectoryOrFile:(NSString *)path{
NSFileManager *fm = [NSFileManager defaultManager];
//浅层遍历(只包含当前路径的子目录)
if ([fm fileExistsAtPath:path]) {
//数组包含的是文件夹的名字
NSArray *arr1 = [fm contentsOfDirectoryAtPath:path error:nil];
NSLog(@"%ld--%@",arr1.count,arr1.firstObject);
}
//深层遍历(包含路径下所有子文件)
// if ([fm fileExistsAtPath:path]) {
// NSArray *arrAll = [fm subpathsOfDirectoryAtPath:path error:nil];
// }
}
//2.拷贝
- (void)copyDirectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
BOOL isSuc = [fm copyItemAtPath:sourcePath toPath:newPath error:&error];
if (isSuc) {
NSLog(@"success");
}else{
NSLog(@"%@",error.localizedDescription);
}
}
//3.移动(剪切)
- (void)moveDiectoryOrFile:(NSString *)sourcePath toNewPath:(NSString *)newPath{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
BOOL isSuc = [fm moveItemAtPath:sourcePath toPath:newPath error:&error];
if (isSuc) {
NSLog(@"success");
}else{
NSLog(@"%@",error.localizedDescription);
}
}
//4.删除
- (void)deleteDirectoryOrFile:(NSString *)path{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
BOOL isSuc = [fm removeItemAtPath:path error:nil];
if (isSuc) {
NSLog(@"success");
}else{
NSLog(@"%@",error.localizedDescription);
}
}
//三.获取文件属性
- (void)attributeForDirectoryOrFile:(NSString *)path{
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSDictionary *dic = [fm attributesOfItemAtPath:path error:&error];
NSLog(@"%@",dic);
}
文件管理NSFileManager的更多相关文章
- iOS-沙盒路径总结、文件管理NSFileManager总结
// // ViewController.m // 沙盒操作 // // Created by mncong on 15/11/26. // Copyright © 2015年 mancong ...
- IOS--文件管理NSFileManager
iOS的沙盒机制.应用仅仅能訪问自己应用文件夹下的文件.iOS不像android.没有SD 卡概念.不能直接訪问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒 ...
- 数据持久化-存取方式总结&应用沙盒&文件管理NSFileManager
iOS应用数据存储的常用方式: 1.XML属性列表 (plist归档) 2.NSUserDefaults (偏好设置) 3.NSKeyedArchiver 归档(加密形式) 4.SQLi ...
- iOS路径沙盒文件管理(转载)
iOS路径沙盒文件管理,看到博主总结的很好,转载过来,原文:http://www.aichengxu.com/view/35264 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文 ...
- iOS开发-文件管理(一)
iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...
- [OC Foundation框架 - 23] 文件管理
A. 目录管理 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 // 1.获取文件属性 NSString *path = @& ...
- IOS 开发之文件管理
一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数据库 ...
- iOS开发-文件管理
iOS学习笔记(十七)--文件操作(NSFileManager) 浅析 RunLoop 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled iOS开发--Swift篇&a ...
- NSString,NSData,NSFileManager常用方法
一.利用NSString类进行文件路径的处理 文件路径格式: NSString *path=@"/Uesrs/apple/testfile.txt" 常用方法汇总: 1.获得组成此 ...
随机推荐
- Python三元运算和lambda表达式
一.三元运算 1.定义:三元运算是if-else 语句的快捷操作,也被称为条件运算. 2.结构: [on_true] if [expression] else [on_false] 3.示例: ...
- 高性能的城市定位API接口
如果不需要精准的定位,还有一种通过IP地址获取当前城市的方法,采用新浪的api接口. <script src="http://int.dpool.sina.com.cn/iplooku ...
- 框架面试题:谈谈我对Spring IOC与DI的理解
IOC是一种叫做“控制反转”的设计思想. 1.较浅的层次——从名字上解析 “控制”就是指对 对象的创建.维护.销毁等生命周期的控制,这个过程一般是由我们的程序去主动控制的,如使用new关键字去创建一个 ...
- keystone部署及操作
目录 一 版本信息 二 部署keystone 三 keystone操作 四 验证 五 创建脚本 六 keystone使用套路总结 一.版本信息 官网http://docs.openstac ...
- ssh时传递环境变量
设置要传递的变量: -o SendEnv=Varname 但是不是每个都能传,受服务器上sshd_config里的下面两个选项的控制: AcceptEnv and PermitUserEnvironm ...
- integer encoding vs 1-hot (py)
https://github.com/szilard/benchm-ml/issues/1 glouppe commented on 7 May 2015 Thanks for the benchma ...
- MVC下的cshtml和aspx页面
MVC中的aspx页面是System.Web.Mvc.ViewPage类的实例. 表示将视图呈现为 Web 窗体页所需的属性和方法. 继承层次结构 System.Object System.Web.U ...
- Reading Text-based Files In ASP.NET
Friday, July 17, 2015 1:43 PM Every time I need to work with the contents of text-based files in an ...
- DataGridView自定义行样式和行标题
定义两个样式对象: //定义两种行样式 private DataGridViewCellStyle m_RowStyleNormal; private DataGridViewCellStyle m_ ...
- Microsoft.Office.Interop.Word.DocumentClass.SaveAs 命令失败
asp.net 常用的生成word功能,代码也是网上常见的,自己本地反复测试过没问题.serves 2003下运行没问题,可是发布到2008上就出错.组件权限已配置,windows目录下temp权限已 ...