//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. 6410中的PWM 定时器

    看了OK6410的手册,感觉晕晕的. 需要整理一下思路. 我觉得主要的知道下面这几个内容吧. 1. 定时器的电路结构. 2. 定时器的工作原理是什么.定时器如何来使用.{使用的时序是什么,在时间轴上各 ...

  2. oracle xe远程访问

    oracle xe其实监听了1521端口 netstat -ano|findstr 只是没请求防火墙权限而已. 手动打开防火墙1521端口 管理员运行下面的命令 本机环境win10 netsh adv ...

  3. Golang简单写文件操作的四种方法

    package main import ( "bufio" //缓存IO "fmt" "io" "io/ioutil" ...

  4. 当一个SQL语句同时出现了where,group by,having,order by的时候,执行顺序和编写顺序

    当一个查询语句同时出现了where,group by,having,order by的时候,执行顺序和编写顺序 1.执行where xx对全表数据做筛选,返回第1个结果集. 2.针对第1个结果集使用g ...

  5. Android selector中的item的顺序

    在selector中,要将默认状态的item放在最后面,因为一旦前面的item满足匹配条件,后面的item就不会去匹配.因此,把默认状态的item放在前面的话,后面的item没有执行的机会

  6. 使用递归函数,输出n个元素的所有子集

    题目描述: 请编写一个递归函数,用来输出n个元素的所有子集.例如,三个元素{a,b,c}的所有子集是:{},{a},{b},{c},{a,c},{ac},{b,c},{a,b,c}. 解题思路: 根据 ...

  7. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

  8. Mac anzhuangxgboost

    2. 从Github库安装XGBoost 第一步, 克隆最新的XGBoost到本地 git clone --recursive https://github.com/dmlc/xgboost 第二步, ...

  9. c# 常规验证基类

    using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions ...

  10. JavaScript跨域调用基于JSON的RESTful API

    1. 基本术语 AJAX(Asynchronous JavaScript And XML,异步JavaScript和XML):AJAX是一种用于创建快速动态网页的技术,通过在后台与服务器进行少量数据交 ...