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. @objc and dynamic

      @objc and dynamic Objective-C runtime visibility and the depths of dynamic dispatch in the modern ...

  2. 安卓app测试之内存分析

    一.内存分析步骤 1.启动App. 2.使用monitor命令打开:ADM(包含DDMS) ->update heap 3.操作app,点几次GC 4.dump heap 5.hprof-con ...

  3. java虚拟机(八)--java性能监控与故障处理工具

    问题定位: 除了个人经验,知识,工具也是很重要的,通过数据进行问题分析,包括:运行日志.异常堆栈.GC日志.线程快照(threaddump/javacore文件 ).堆转储快照(heapdump/hp ...

  4. Scrapy 组件的具体用法

    一.Spider 用法 在 Scrapy 中,要抓取网站的链接配置.抓取逻辑.解析逻辑都是在 Spider 里完成的.Spider 的一些基础属性和基础方法: name:爬虫名字,Spider的名字定 ...

  5. 诊断:expdp导出时遇到错误ORA-31693和ORA-00922

    11.2.0.1使用数据泵expdp导出时,如果使用parallel,可能会遇到 ORA-: Table data object "OWNER"."TABLE" ...

  6. C++ Error C2662 cannot convert 'this' pointer from 'const *'

    ---恢复内容开始--- 这个错误在于一点:常量对象只能调用常量成员(函数\变量),不能调用非常量成员.另一方面,非常量对象,既可以调用常量成员,又可以调用非常量成员. class A { publi ...

  7. 支持向量机(SVM)原理浅析

    因为网页博客输入公式很麻烦,所以就在word上面写了,然后截图发上来. 后续关于SVM和FC在深度学习当中得使用对比分析,我再补充.

  8. session对象的使用

    session对象的使用 制作人:全心全意 session在网络中被称为会话.由于HTTP协议是一种无状态协议,也就是当一个客户向服务器发出请求,服务器接收请求,并返回响应后,该连接就结束了,而服务器 ...

  9. BC in fluent

    Boundary conditions in Fluent Table of Contents 1. Boundary Conditions (BC) 1.1. Turbulence Paramete ...

  10. nginx4win10 文件下载服务器

    默认root是Nginx下目录html. 我们在其目录下新建download目录,然后在该目录下copy几个供下载的文件. 在浏览器输入http://localhost:9001/download/y ...