文件管理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.获得组成此 ...
随机推荐
- C#操作SQLIte数据。
using System; using System.Data; using System.Text.RegularExpressions; using System.Xml; using Syste ...
- Spring Bean定义的三种方式
<!--Spring容器启动配置(web.xml文件)--> <context-param> <param-name>contextConfigLocation&l ...
- window - BOM对象
Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外 ...
- bootstrap设计网站中添加代码高亮插件
这款插件的名字叫做google-code-prettify 使用该插件之前的效果: 使用插件之后的效果: 接下来说步骤: (1)下载两个文件 http://codecloud.sinaapp.com/ ...
- Service和IntentService的区别
不知道大家有没有和我一样,以前做项目或者练习的时候一直都是用Service来处理后台耗时操作,却很少注意到还有个IntentService,前段时间准备面试的时候看到了一篇关于IntentServic ...
- 推荐一款基于XNA的开源游戏引擎《Engine Nine》
一.前沿导读 XNA是微软基于.Net部署的下一代3D/2D游戏开发框架,其实XNA严格来说类似下一代的DirectX,当然不是说XNA会取代DirectX,但是基于XNA我们对于面向XBOX360, ...
- http请求和返回的head字段
一,http请求分请求首部字段,通用首部字段,实体首部字段.http响应包含响应首部字段,通用首部字段,实体首部字段. 二,http1.1定义了47种首部字段.1,通用首部字段:cache-contr ...
- [GO]将随机生成的四位数字拆分后放到一个切片里
package main import ( "math/rand" "time" "fmt" ) func InitData(p *int) ...
- PC/APP/H5三端测试的相同与不同
随着手机应用的不断状态,同一款产品的移动端应用市场占相较PC端也越来越大,那么app与PC端针对这些产品的测试有什么相同与不同之处呢?总结如下: 首先谈一谈相同之处: 一,针对同一个系统功能的测试,三 ...
- orcad找不到dll
如果运行Capture.exe找不到cdn_sfl401as.dll,如果运行allegro.exe找不到cnlib.dll,(上面俩个库文件都在C:/Cadence/SPB_16.3/tools/b ...