createFileAtPath //创建文件

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath = [NSString stringWithFormat:@"%@/file1.txt",NSHomeDirectory()];
NSString *strdata = @"test"; bool bRet = [fm createFileAtPath:strpath contents:strdata attributes:nil];
if(!bRet)
{
NSLog(@"create file error");
}

copyItemAtPath  //拷贝文件

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/file1.txt",NSHomeDirectory()];
NSString *strpath2 = [NSString stringWithFormat:@"%@/file2.txt",NSHomeDirectory()]; bool bRet = [fm copyItemAtPath:strpath1 toPath:strpath2 error:nil];
if(!bRet)
{
NSLog(@"copy file error");
}

moveItemAtPath  //移动文件

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/file1.txt",NSHomeDirectory()];
NSString *strpath2 = [NSString stringWithFormat:@"%@/file2.txt",NSHomeDirectory()]; bool bRet = [fm moveItemAtPath:strpath1 toPath:strpath2 error:nil];
if(!bRet)
{
NSLog(@"move file error");
}

removeItemAtPath   //删除文件

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/file1.txt",NSHomeDirectory()]; bool bRet = [fm removeItemAtPath:strpath1 error:nil];
if(!bRet)
{
NSLog(@"delete file error");
}

attributesOfItemAtPath   //获取文件属性, 文件大字。返回NSDictionary

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/log.txt",NSHomeDirectory()]; NSDictionary *dic = [fm attributesOfItemAtPath:strpath1 error:nil];
NSLog(@"%@",dic);

currentDirectoryPath   //获取当前文件夹

       NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath = [fm currentDirectoryPath];
NSLog(@"%@",strpath);

createDirectoryAtPath   //创建文件夹

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/testdir",NSHomeDirectory()];
bool bRet = [fm createDirectoryAtPath:strpath1 withIntermediateDirectories:NO attributes:nil error:nil];
if(!bRet)
{
NSLog(@"create dir error");
}

fileExistsAtPath    //推断文件或文件夹是否存在

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/testdir",NSHomeDirectory()];
bool bRet = [fm fileExistsAtPath:strpath1];
if(!bRet)
{
NSLog(@"no file exist");
}
else
{
NSLog(@"file exist");
}

enumeratorAtPath   //枚举文件夹,将子文件夹所有枚举

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/Desktop",NSHomeDirectory()];
NSDirectoryEnumerator *dirs = [fm enumeratorAtPath:strpath1]; NSString *dir;
while (dir=[dirs nextObject]) {
NSLog(@"%@",dir);
}

contentsOfDirectoryAtPath   //枚举文件夹,不枚举子文件夹

        NSFileManager *fm = [NSFileManager defaultManager];
NSString *strpath1 = [NSString stringWithFormat:@"%@/Desktop",NSHomeDirectory()];
NSArray *dirs = [fm contentsOfDirectoryAtPath:strpath1 error:nil]; NSString *dir;
for (dir in dirs)
{
NSLog(@"%@",dir);
}

Objective-C NSFileManager 文件管理总结的更多相关文章

  1. iOS - OC NSFileManager 文件管理

    前言 @interface NSFileManager : NSObject @interface NSFileHandle : NSObject <NSSecureCoding> NSF ...

  2. NSFileManager文件管理

    前提,用到的东东: 1.文件数据类:NSData类型(二进制) 1)作用:专门用于将数据封装成二进制的类.数据(文本,图片,音频,视频....)  ==> NSData类型的对象 2)编码方式: ...

  3. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  4. Swift\本地文件管理

    转载自:http://www.coloroud.com/2015/06/01/Swift-File-Manager/ 开头 看来Swift这趟浑水是非干不可,既然如此,那索性就来的彻底吧,来一次全方位 ...

  5. iOS核心面试题

    1,请简述你对协议的理解?    protocol无论是在那个领域都是一种约束,规范.在OC中的协议主要用于在各个类之间进行回调传值. 协议有 委托方,代理方, 委托方是协议的制定者,需要声明协议的方 ...

  6. iOS开发中常用的单例

    定义:一个类的对象,无论在何时创建.无论创建多少次,创建出来的对象都是同一个对象. 使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中.   关键代码: + (instan ...

  7. 浅谈iOS中的单例模式

    iOS中的单例模式     就我本身理解而言,我认为的单例:单例在整个工程中,就相当于一个全局变量,就是不论在哪里需要用到这个类的实例变量,都可以通过单例方法来取得,而且一旦你创建了一个单例类,不论你 ...

  8. UI基础:DataPersistent.沙盒

    沙盒是系统为每一个应用程序生成的一个特定文件夹,文件夹的名字由一个十六进制数据组成,每一个应用程序的沙盒文件名都是不一样的,是由系统随机生成的. 沙盒主目录: NSString *homePath = ...

  9. IOS开发-视频,音频,录音简单总结

    /***** * 1. 视频播放 * *  @格式:mp4 mov m4v m2v 3gp 3g2 * *  @系统框架使用:#import <MediaPlayer/MediaPlayer.h ...

随机推荐

  1. 对比hive和mysql查询汇总

    由于底层的处理机制大不相同,hive和mysql在查询上还是有较大差异的! 单个表的select操作 最简单的查询 ,字段2 frome 表名 where 字段 [not]in(元素1,元素2): 例 ...

  2. CodeFrist基础_迁移更新数据

    一丶自动迁移 第一次启用迁移:NeGet-->Enable-Migrations public DemoDbContext() : base("name=ConncodeFirst&q ...

  3. MySQL:INSERT ... UPDATE

    在 INSERT 语句末尾指定ON DUPLICATE KEY UPDATE时,如果插入的数据会导致表中的 UNIQUE 索引或 PRIMARY KEY 出现重复值,则会对导致重复的数据执行 UPDA ...

  4. cmd命令安装、卸载、启动和停止Windows Service

    1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft.NET ...

  5. 洛谷——P1229 遍历问题

    P1229 遍历问题 题目描述 我们都很熟悉二叉树的前序.中序.后序遍历,在数据结构中常提出这样的问题:已知一棵二叉树的前序和中序遍历,求它的后序遍历,相应的,已知一棵二叉树的后序遍历和中序遍历序列你 ...

  6. Linux命令rsync使用总结

    详细用法见:https://www.cnblogs.com/oboth-zl/articles/10334754.html rsync命令简介 主要用于数据同步.备份和镜像,除了本地使用之外,也可以通 ...

  7. buf.writeIntBE()函数详解

    buf.writeIntBE(value, offset, byteLength[, noAssert]) buf.writeIntLE(value, offset, byteLength[, noA ...

  8. 51nod1128 正整数分组V2

    [题解] 二分一个最大值,check一下分出来的组数是否小于等于k即可. #include<cstdio> #include<algorithm> #define LL lon ...

  9. 【Codeforces 364A】Matrix

    [链接] 我是链接,点我呀:) [题意] 让你求出b[i][j]=s[i]*s[j]规则构成的矩阵 的所有子矩阵中子矩阵的和为a的子矩阵的个数 [题解] (x,y,z,t) 会发现它的和就是sum(x ...

  10. ACE in Action

    ACE in Action https://cdn.xgqfrms.xyz/web-ide/index.html TinyMCE https://panjiachen.github.io/vue-el ...