文件管理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.获得组成此 ...
随机推荐
- Centos手动编译安装vim8
系统:CentOs6.9 gcc版本:5.1.0 github上拉取vim工程之后,cd src,执行一下命令: $ ./configure $ sudo make gcc -c -I. -Iprot ...
- flask+jsonp跨域前后台交互(接口初体验)
1 # -*- coding: utf-8 -*- 2 from flask import Flask, jsonify 3 import psutil, time,json 4 5 app = Fl ...
- 张超超OC基础回顾_05 property修饰符,id类型,instancetype。。。
一.property 如果给一个属性同时提供了getter/setter方法, 那么我们称这个属性为可读可写属性 如果只提供了getter方法, 那么我们称这个属性为只读属性 如果只提供了setter ...
- 566. Reshape the Matrix矩阵重排
[抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
- Docker学习笔记_安装和使用Apache
一.准备 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu18.04 3.账号:docker 二.安装 1.搜索镜像 ...
- weblogic参数说明
公司有个项目,部署在weblogic8.1上之后,发现比在tomcat下慢很多,经过分析排查,原因是web应用的WEB-INF下的weblogic.xml里的参数设置不合理(使用默认值有时并非最佳值) ...
- [存储过程]中的事务(rollback)回滚
在编写SQL Server 事务相关的存储过程代码时,经常看到下面这样的写法: begin tran update statement 1 ... update statement 2 ... del ...
- vis用于做3D图表的js插件
vis.js用于做3D图表:(浏览网站需要FQ)实例:http://visjs.org/graph3d_examples.html代码下载:https://github.com/almende/vis
- Python基础-2
目录: 1.列表.元组操作 2.字符串操作 3.字典操作 4.集合操作 5.文件操作 6.字符编码与转码 一.列表.元组操作 定义列表 names = ['Freeman',"Jack&qu ...
- Python基础入门-元祖
其实,元组合列表的特性和使用几乎差不太多,今天我们重点来看下元组的一些操作和使用. 1.元祖的定义和特点 定义:元组是以小括号包围,元素以逗号分隔,不可变的序列之一. 特点: 1)元祖内的元素不可以增 ...