转自:http://blog.csdn.net/zhuzhihai1988/article/details/7904333
[self.fileManagerfileExistsAtPath:isDirectory:];
用于执行一般的文件系统操作 (reading and writing is done via NSData, et. al.).
主要功能包括:从一个文件中读取数据;向一个文件中写入数据;删除文件;复制文件;移动文件;比较两个文件的内容;测试文件的存在性;读取/更改文件的属性... ...
Just alloc/init an instance and start performing operations. Thread safe.
- 常见的NSFileManager处理文件的方法如下:
NSFileManager *fileManager = [[NSFileManager alloc]init]; //最好不要用defaultManager。
NSData *myData = [fileManager contentsAtPath:path]; // 从一个文件中读取数据
[fileManager createFileAtPath:path contents:myData attributes:dict];//向一个文件中写入数据,属性字典允许你制定要创建
[fileManager removeItemAtPath:path error:err];
[fileManager moveItemAtPath:path toPath:path2 error:err];
[fileManager copyItemAtPath:path toPath:path2 error:err];
[fileManager contentsEqualAtPath:path andPath:path2];
[fileManager fileExistsAtPath:path]; ... ...
- 常见的NSFileManager处理目录的方法如下:
[fileManager currentDirectoryPath];
[fileManager changeCurrentDirectoryPath:path];
[fileManager copyItemAtPath:path toPath:path2 error:err];
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:err];
[fileManager fileExistsAtPath:path isDirectory:YES];
[fileManager enumeratorAtPath:path]; //获取目录的内容列表。一次可以枚举指定目录中的每个文件。 ... ...Has a delegate with lots of “should” methods (to do an operation or proceed after an error).
And plenty more. Check out the documentation.
1、文件的创建
-(IBAction) CreateFile { //对于错误信息 NSError *error; // 创建文件管理器 NSFileManager *fileMgr = [NSFileManager defaultManager]; //指向文件目录 NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
[[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder", NSHomeDirectory()] attributes:nil]; // File we want to create in the documents directory我们想要创建的文件将会出现在文件目录中 // Result is: /Documents/file1.txt结果为:/Documents/file1.txt NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"]; //需要写入的字符串 NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com"; //写入文件 [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; //显示文件目录的内容 NSLog(@"Documentsdirectory: %@",[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]); |
2、对文件重命名
对一个文件重命名 想要重命名一个文件,我们需要把文件移到一个新的路径下。下面的代码创建了我们所期望的目标文件的路径,然后请求移动文件以及在移动之后显示文件目录。 //通过移动该文件对文件重命名 NSString *filePath2= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"]; //判断是否移动 if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES) NSLog(@"Unable to move file: %@", [error localizedDescription]); //显示文件目录的内容 NSLog(@"Documentsdirectory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]); |
3、删除一个文件
这些示例能教你的,仅仅只是文件处理上的一些皮毛。想要获得更全面、详细的讲解,你就需要掌握NSFileManager文件的知识。 |
4、删除目录下所有文件
//获取文件路径 NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [document stringByAppendingPathComponent:@"Attchments"];
[manager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil]; } } --清除附件 |
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath])
{
//do some thing
}
附:
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"data.plist"];
}
============================================================================
NSFileHandle类允许更有效地使用文件。
可以实现如下功能:
1、打开一个文件,执行读、写或更新(读写)操作;
2、在文件中查找指定位置;
3、从文件中读取特定数目的字节,或将特定数目的字节写入文件中
另外,NSFileHandle类提供的方法也可以用于各种设备或套接字。一般而言,我们处理文件时都要经历以下三个步骤:
1、打开文件,获取一个NSFileHandle对象(以便在后面的I/O操作中引用该文件)。
2、对打开文件执行I/O操作。
3、关闭文件。
NSFileHandle *fileHandle = [[NSFileHandle alloc]init];
fileHandle = [NSFileHandle fileHandleForReadingAtPath:path]; //打开一个文件准备读取
fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
fileData = [fileHandle availableData]; // 从设备或者通道返回可用的数据
fileData = [fileHandle readDataToEndOfFile];
[fileHandle writeData:fileData]; //将NSData数据写入文件
[fileHandle closeFile]; //关闭文件 ... ...注:NSFileHandle类没有提供创建文件的功能,所以必须使用NSFileManager来创建文件
相关推荐
文件無紙化,可節省大量的发行成本,ISO文件管理系統使企業推動ISO文件管理、通過認證收到事半功倍之效。 适应性强: 可自行定義和维护分类结构体系、可以自行新增或移动文件夹,同时適用於ISO9000和ISO14000,能应...
iso9000企业文件管理系统试用版,大家试试看看好不好用。
ISO9000系列标准是由ISO9000质量管理体系(基础和术语)、ISO9001质量管理体系(要求)、ISO9004质量管理体系(业绩改进指南)、ISO9011质量和环境审核指南、ISO10012测量控制系统构成,其中ISO9001和ISO9004是其...
ISO9000质量管理体系2015新版,不再强制要求《质量手册》和《程序文件》这类难以理解的文件形式,而统一用“形成文件的信息”取而代之,对文件的载体是纸质还是电子文档或软件信息系统都予以认可和支持,明显可以...
ISO41001-2018设施管理.管理系统.带使用指南的要求
ISO文件系统操作说明,教学手册,方便参考
ISO 27001信息安全管理体系实施案例及文件集 ISO 27001全套文件 ISO 27001英版标准及国标转化标准全套 ISO 27001中文版全套 信息安全风险评估表 ISMS信息安全管理体系案例 ISMS信息安全管理体系内部审核员培训(ISO...
ISO27001-2013信息安全管理体系(管理手册+程序文件+记录表格模版)资料汇总,共159份。 访问控制制度 信息安全管理体系-管理手册 文件控制制度 记录控制制度 内部审核控制制度 管理评审控制制度 信息安全风险评估...
ISO9000品质管理系统ISO9000品质管理系统.pptx
ISO9000品质管理系统-ISO9000品质管理系统.pptx
│ [XX-b-23]门禁系统管理程序.doc │ [XX-b-24]网络设备安全配置管理程序.doc │ [XX-b-25]计算机管理程序.doc │ [XX-b-26]电子邮件管理程序.doc │ [XX-b-27]恶意软件管理程序.doc │ [XX-b-28]可移动介质管理...
本文档系统的介绍了ISO27001认证过程中所需要的人员管理制度、设备日常巡检制度、机房管理制度、信息安全事件管理制度、业务连续性管理制度等
ISO9001质量管理体系系统培训(PPT 162页).pptx
ISO4001体系文件管理方案管理程序.
ISO 37001-2016中文 反贿赂管理系统--要求与使用指南.pdf
ISO IEC 27001-2022英文+中文 信息安全管理系统要求.pdf
ISO 27001信息安全管理体系文件系统安全管理规定.docx
文件系统是用来管理和组织保存在磁盘驱动器上数据的系统软件 2.Linux的文件系统 Linux系统采用虚拟文件系统技术(VFS)、结构采用倒立树型、VFS使Linux支持以下文件系统: EXT2: 二次扩展 EXT3: 三次扩展...
ISO 37002-2021中文 举报管理制度--准则.pdf