文件管理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.获得组成此 ...
随机推荐
- selector在color和drawable目录下的区别
selector作为drawable资源时,放于drawable目录下,并且item指定android:drawable属性,引用使用@drawable而不是@color selector作为colo ...
- 599. Minimum Index Sum of Two Lists两个餐厅列表的索引和最小
[抄题]: Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of fa ...
- Hadoop完全分布式环境搭建(四)——基于Ubuntu16.04安装和配置Hadoop大数据环境
[系统环境] [安装配置概要] 1.上传hadoop安装文件到主节点机器 2.给文件夹设置权限 3.解压 4.拷贝到目标文件夹 放在/opt文件夹下,目录结构:/opt/hadoop/hadoop-2 ...
- Apache htpasswd命令
一.简介 htpasswd是apache的一个工具,该工具主要用于建立和更新存储用户名.密码的文本文件,主要用于对基于http用户的认证. 二.语法 Usage: htpasswd [-cimBdps ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- CodeBlocks调试功能(转)
转自:迂者-贺利坚 http://blog.csdn.net/sxhelijian/article/details/15026159 示例代码: #include <iostream> u ...
- MySQL事务隔离级别测试实例
https://www.cnblogs.com/huanongying/p/7021555.html MySQL事务隔离级别 事务隔离级别 脏读 不可重复读 幻读 读未提交(read-uncommit ...
- Socket编程(c语言示例)
转自:http://blog.csdn.net/dxpqxb/article/details/8166423 前言 Socket可以看成在两个程序进行通讯连接中的一个端点,是连接应用程序和网络驱动程序 ...
- Diameter协议摘要
---------选择同学整理文档 1. 协议概述 Diameter协议主要为应用程序提供认证.鉴权.计费框架,即AAA,并支持本地AAA和漫游场景下的AAA. 1.1. 特点介绍 以前的AAA ...
- Async异步委托
/// <summary> /// 异步委托 /// </summary> public delegate void AsyncHandler(); public static ...