IOS文件系统及其相关操作(NSFileManager,NSFileHandle)
How do you get the paths to these special sandbox directories?
NSSearchPathDirectory directory, // see below
NSSearchPathDomainMask domainMask, // NSUserDomainMask
BOOL expandTilde // YES
);
NSDocumentsDirectory, NSCachesDirectory, NSAutosavedInformationDirectory, etc.
用于执行一般的文件系统操作 (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"]; NSFileManager *manager = [NSFileManager defaultManager]; if(![manager contentsOfDirectoryAtPath:path error:nil]){ [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来创建文件。
IOS文件系统及其相关操作(NSFileManager,NSFileHandle)的更多相关文章
- PHP文件系统处理相关操作
<?php/* PHP文件系统处理 * 所有文件处理都是使用系统函数完成的. * 是基于Linux/Unix系统为模型 * * 文件系统处理的作用: * 1. 所有的项目离不开文件处理 * 2. ...
- 详解ios文件系统文件目录读写操作-备用
iPhone文件读写系统操作教程是本文要介绍的内容,对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(所谓sandbox).一个app发布到iPhone上后,它得目录结构如下: ...
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...
- Linux之文档与目录结构 目录的相关操作 Linux的文件系统
Linux之文档与目录结构 Linux文件系统结构 Linux目录结构的组织形式和Windows有很大的不同.首先Linux没有“盘(C盘.D盘.E盘)”的概念.已经建立文件系统的硬盘分区被挂载到 ...
- iOS的I/O操作
一般而言,处理文件时都要经历以下四个步骤: 1.创建文件 2.打开文件,以便在后面的I/O操作中引用该文件 3.对打开的文件执行I/O操作(读取.写入.更新) 4.关闭文件 iOS中,对文件常见的处理 ...
- linux下进程相关操作
一.定义和理解 狭义定义:进程是正在运行的程序的实例. 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动. 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的 ...
- 从零自学Hadoop(24):Impala相关操作上
阅读目录 序 数据库相关 表相关 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...
- 利用JAVA API远程进行HDFS的相关操作
学习HDFS有一段时间了,现在把自己总结的HDFS的相关操作代码展示给大家. 主要有HDFS的增删改查,文件的追加,windows本地文件的上传,hdfs文件的下载,文件重命名,创建目录,文件是否存在 ...
- python文件相关操作
Python文件相关操作 打开文件 打开文件,采用open方法,会将文件的句柄返回,如下: f = open('test_file.txt','r',encoding='utf-8') 在上面的代码中 ...
随机推荐
- wordpress配置通过IP直接访问及apache的配置
wordpress配置通过IP直接访问 环境: 操作系统:centos6.5 yum安装lamp环境: yum -y install mysql mysql-server php php-mysql ...
- File /data/binlog/mysql-bin.index' not found (Errcode: 13)
[问题] 需要开启bin-log备份/恢复数据库,但是因为本身bin-log保存的位置存储太小,并且归类性也不好,所以自己新创建了/data/binlog来保存二进制日志 在/etc/my.cnf增加 ...
- 【前端开发】移动端适配方案js,rem单位转换,640设计稿20px=1rem
! function() { var style = document.createElement("STYLE"), docEl = document.documentEleme ...
- Django项目之cookie+session
原文:https://www.cnblogs.com/sss4/p/7071334.html HTTP协议 是短连接.且状态的,所以在客户端向服务端发起请求后,服务端在响应头 加入cokie响应给浏览 ...
- 深入理解java虚拟机-00
这本书买了有两年了,只有买回来翻了两页...今天电脑有点卡,游戏玩不了了,就来看看这本书. 首先看了序言,这本书是第二版,讲解的jdk版本是1.7,现在公司用的1.8,而且1.8的改动也挺大的,不过在 ...
- linux pwd指令C实现
linux pwd指令C实现 研究实现pwd所需的系统调用 我们可以通过man命令和grep命令来获取我们所需要的系统调用函数信息 man chdir Linux pwd命令用于显示工作目录. 执行p ...
- Spring Boot 入门案例与配置说明
一.Spring Boot简介 官网地址:http://spring.io/projects/spring-boot Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用 ...
- 1195: [HNOI2006]最短母串
思路:好像以前谁问过我这题... 状个压就好啦, 把包含在其他串中的字符串删掉, 预处理除每两个字符串之间的关系, dp[ state ][ i ] 表示在state的状态下, 最后一个字符串是第i ...
- Java中static块执行时机
Java中static块执行时机 演示例子 在使用static进行初始化的操作,怎么也执行不了!代码如下: public class StaticDemo { public static final ...
- BZOJ.3994.[SDOI2015]约数个数和(莫比乌斯反演)
题目链接 \(Description\) 求\[\sum_{i=1}^n\sum_{j=1}^md(ij)\] \(Solution\) 有结论:\[d(nm)=\sum_{i|d}\sum_{j|d ...