createFileAtPath //创建文件

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

copyItemAtPath  //拷贝文件

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

moveItemAtPath  //移动文件

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

removeItemAtPath   //删除文件

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

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

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

currentDirectoryPath   //获取当前文件夹

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

createDirectoryAtPath   //创建文件夹

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

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

  1. NSFileManager *fm = [NSFileManager defaultManager];
  2. NSString *strpath1 = [NSString stringWithFormat:@"%@/testdir",NSHomeDirectory()];
  3. bool bRet = [fm fileExistsAtPath:strpath1];
  4. if(!bRet)
  5. {
  6. NSLog(@"no file exist");
  7. }
  8. else
  9. {
  10. NSLog(@"file exist");
  11. }

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

  1. NSFileManager *fm = [NSFileManager defaultManager];
  2. NSString *strpath1 = [NSString stringWithFormat:@"%@/Desktop",NSHomeDirectory()];
  3. NSDirectoryEnumerator *dirs = [fm enumeratorAtPath:strpath1];
  4.  
  5. NSString *dir;
  6. while (dir=[dirs nextObject]) {
  7. NSLog(@"%@",dir);
  8. }

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

  1. NSFileManager *fm = [NSFileManager defaultManager];
  2. NSString *strpath1 = [NSString stringWithFormat:@"%@/Desktop",NSHomeDirectory()];
  3. NSArray *dirs = [fm contentsOfDirectoryAtPath:strpath1 error:nil];
  4.  
  5. NSString *dir;
  6. for (dir in dirs)
  7. {
  8. NSLog(@"%@",dir);
  9. }

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. 由DB2分页想到的,关于JDBC ResultSet 处理大数据量

    最近在处理DB2 ,查询中,发现如下问题.如果一个查询 count(*),有几十万行,分页如何实现 select row_number() over (order by fid desc ) as r ...

  2. 06Microsoft SQL Server 完整性约束

    Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...

  3. Codeforces985E. Pencils and Boxes (单调队列)

    题意:n个数 把他们放进一些盒子里 每个盒子最少放k个数 且最小和最大的差不能大于d 题解:显然排个序 对于当前点 存一下前面有哪些节点可以当作结尾 那么就可以枚举这些点的下一个点作为起点能否和当前点 ...

  4. spark学习(1)---dataframe操作大全

    一.dataframe操作大全 https://blog.csdn.net/dabokele/article/details/52802150 https://www.jianshu.com/p/00 ...

  5. Python之IO编程

    前言:由于程序和运行数据是在内存中驻留的,由CPU这个超快的计算核心来执行.当涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口.由于CPU和内存的速度远远高于外设的速度,那么在IO编程中就存在 ...

  6. Hadoop Mapreduce 中的Partitioner

    Partitioner的作用的对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,Partitioner直接影响Reduce阶段的负载均衡. MapReduce提供了 ...

  7. Linux无人值守安装系统

    yum install dhcp -yvim /etc/dhcp/dhcpd.conf--------------------------------------------- allow booti ...

  8. vue SSR 部署详解

    先用vue cli初始化一个项目吧. 输入命令行开始创建项目: vue create my-vue-ssr 记得不要选PWA,不知为何加了这个玩意儿就报错. 后续选router模式记得选 histor ...

  9. js之标签操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. js之DOM直接操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...