Objective-C NSFileManager 文件管理总结
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 文件管理总结的更多相关文章
- iOS - OC NSFileManager 文件管理
前言 @interface NSFileManager : NSObject @interface NSFileHandle : NSObject <NSSecureCoding> NSF ...
- NSFileManager文件管理
前提,用到的东东: 1.文件数据类:NSData类型(二进制) 1)作用:专门用于将数据封装成二进制的类.数据(文本,图片,音频,视频....) ==> NSData类型的对象 2)编码方式: ...
- 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)
========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...
- Swift\本地文件管理
转载自:http://www.coloroud.com/2015/06/01/Swift-File-Manager/ 开头 看来Swift这趟浑水是非干不可,既然如此,那索性就来的彻底吧,来一次全方位 ...
- iOS核心面试题
1,请简述你对协议的理解? protocol无论是在那个领域都是一种约束,规范.在OC中的协议主要用于在各个类之间进行回调传值. 协议有 委托方,代理方, 委托方是协议的制定者,需要声明协议的方 ...
- iOS开发中常用的单例
定义:一个类的对象,无论在何时创建.无论创建多少次,创建出来的对象都是同一个对象. 使用场景:当有一些数据需要共享给别的类的时候,就可以把这些数据保存在单例对象中. 关键代码: + (instan ...
- 浅谈iOS中的单例模式
iOS中的单例模式 就我本身理解而言,我认为的单例:单例在整个工程中,就相当于一个全局变量,就是不论在哪里需要用到这个类的实例变量,都可以通过单例方法来取得,而且一旦你创建了一个单例类,不论你 ...
- UI基础:DataPersistent.沙盒
沙盒是系统为每一个应用程序生成的一个特定文件夹,文件夹的名字由一个十六进制数据组成,每一个应用程序的沙盒文件名都是不一样的,是由系统随机生成的. 沙盒主目录: NSString *homePath = ...
- IOS开发-视频,音频,录音简单总结
/***** * 1. 视频播放 * * @格式:mp4 mov m4v m2v 3gp 3g2 * * @系统框架使用:#import <MediaPlayer/MediaPlayer.h ...
随机推荐
- 由DB2分页想到的,关于JDBC ResultSet 处理大数据量
最近在处理DB2 ,查询中,发现如下问题.如果一个查询 count(*),有几十万行,分页如何实现 select row_number() over (order by fid desc ) as r ...
- 06Microsoft SQL Server 完整性约束
Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...
- Codeforces985E. Pencils and Boxes (单调队列)
题意:n个数 把他们放进一些盒子里 每个盒子最少放k个数 且最小和最大的差不能大于d 题解:显然排个序 对于当前点 存一下前面有哪些节点可以当作结尾 那么就可以枚举这些点的下一个点作为起点能否和当前点 ...
- spark学习(1)---dataframe操作大全
一.dataframe操作大全 https://blog.csdn.net/dabokele/article/details/52802150 https://www.jianshu.com/p/00 ...
- Python之IO编程
前言:由于程序和运行数据是在内存中驻留的,由CPU这个超快的计算核心来执行.当涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口.由于CPU和内存的速度远远高于外设的速度,那么在IO编程中就存在 ...
- Hadoop Mapreduce 中的Partitioner
Partitioner的作用的对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,Partitioner直接影响Reduce阶段的负载均衡. MapReduce提供了 ...
- Linux无人值守安装系统
yum install dhcp -yvim /etc/dhcp/dhcpd.conf--------------------------------------------- allow booti ...
- vue SSR 部署详解
先用vue cli初始化一个项目吧. 输入命令行开始创建项目: vue create my-vue-ssr 记得不要选PWA,不知为何加了这个玩意儿就报错. 后续选router模式记得选 histor ...
- js之标签操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js之DOM直接操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...