//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的更多相关文章

  1. iOS-沙盒路径总结、文件管理NSFileManager总结

    // //  ViewController.m //  沙盒操作 // //  Created by mncong on 15/11/26. //  Copyright © 2015年 mancong ...

  2. IOS--文件管理NSFileManager

    iOS的沙盒机制.应用仅仅能訪问自己应用文件夹下的文件.iOS不像android.没有SD 卡概念.不能直接訪问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒 ...

  3. 数据持久化-存取方式总结&应用沙盒&文件管理NSFileManager

    iOS应用数据存储的常用方式:  1.XML属性列表   (plist归档)  2.NSUserDefaults (偏好设置)  3.NSKeyedArchiver  归档(加密形式)  4.SQLi ...

  4. iOS路径沙盒文件管理(转载)

    iOS路径沙盒文件管理,看到博主总结的很好,转载过来,原文:http://www.aichengxu.com/view/35264 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文 ...

  5. iOS开发-文件管理(一)

    iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...

  6. [OC Foundation框架 - 23] 文件管理

    A. 目录管理 NSFileManager*manager = [NSFileManagerdefaultManager];//单例模式 // 1.获取文件属性 NSString *path = @& ...

  7. IOS 开发之文件管理

    一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数据库 ...

  8. iOS开发-文件管理

    iOS学习笔记(十七)--文件操作(NSFileManager) 浅析 RunLoop 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled iOS开发--Swift篇&a ...

  9. NSString,NSData,NSFileManager常用方法

    一.利用NSString类进行文件路径的处理 文件路径格式: NSString *path=@"/Uesrs/apple/testfile.txt" 常用方法汇总: 1.获得组成此 ...

随机推荐

  1. itemize,enumerate,description 用法【LaTeX 使用】

    itemize和enumerate还有description 是LaTeX里列举的三种样式,分别讲一些使用技巧.itemize(意为分条目):\begin{itemize}\item[*] a\ite ...

  2. arm上sd卡热插拔问题的解决:

    首先,保证sd卡驱动是完好,但是sd卡却无法热插拔或者无法识别. 刚开始我的板子上,sd是能够读取的,但是却不支持热插拔,看了几天sd驱动,找到了问题的原因,是驱动中硬件引脚相关设置的问题,具体根绝个 ...

  3. SQL和NoSQL

    SQL和NoSQL 目前的数据库系统非常多,有传统的关系型的数据库系统(又被称为SQL数据库系统),有最近几年流行起来的NoSQL数据库系统.其中NoSQL数据库系统又分为很多种不同的类型,根据各个系 ...

  4. Python代码的编译

    Python代码的编译 Python代码在解释执行之前,是会被编译成.pyc或者.pyo文件的,它们是中间字节码表示的文件,之后Python虚拟机才会去解释执行它们. 1.pyc文件 ======== ...

  5. 判断Android系统net和wap接入点的开发实例

    判断Android系统net和wap接入点的开发实例 分类标签: Activity   我们使用Android设备连接网络时,如果是wap接入点就需要设置代理,而电信和移动联通的代理并不相同,移动和联 ...

  6. Stream01 定义、迭代、操作、惰性求值、创建流、并行流、收集器、stream运行机制

    1 Stream Stream 是 Java 8 提供的一系列对可迭代元素处理的优化方案,使用 Stream 可以大大减少代码量,提高代码的可读性并且使代码更易并行. 2 迭代 2.1 需求 随机创建 ...

  7. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  8. C++ 输出精度和输出小数点位数

    有时候需要调节小数点的精度或者位数 #include<iostream> #include<iomanip> using namespace std; //设置数据精度 set ...

  9. Web API集成Azure AD认证

    1.声明的介绍 基于角色的授权管理,适用于角色变化不大,并且用户权限不会频繁更改的场景. 在更复杂的环境下,仅仅通过给用户分配角色并不能有效地控制用户访问权限. 基于声明的授权有许多好处,它使认证和授 ...

  10. PAT 1017 Queueing at Bank (25) (坑题)

    Suppose a bank has K windows open for service. There is a yellow line in front of the windows which ...