iOS - 操作文件目录的方法
转:http://blog.csdn.net/marujunyy/article/details/11579183
使用目录的常用方法:
- //获取当前目录
- - (NSString *)currentDirectoryPath
- //更改当前目录
- - (BOOL)changeCurrentDirectoryPath:(NSString *)path
- //复制目录或文件
- - (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
- //创建一个新的目录
- - (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error
- //测试文件是不是目录
- - (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory
- //列出目录下的内容(不会递归)
- - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
- //枚举目录内容(会递归)
- - (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)path
- //删除一个文件,文件夹,链接
- - (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
- //移动(重命名)目录到一个指定的路径
- - (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error
- //操作目录
- NSFileManager *fm = [[NSFileManager alloc]init];
- NSString *dirName = @"testdir";
- NSString *path = [fm currentDirectoryPath]; //获取当前目录路径.
- //新建目录
- if ([fm createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:NULL] == NO) {
- NSLog(@"Couldn't create dir");
- }
- //重命名目录
- if ([fm moveItemAtPath:dirName toPath:@"newdir" error:NULL] == NO) {
- NSLog(@"Directory rename failed");
- }
- //修改路径到新目录
- if ([fm changeCurrentDirectoryPath:@"newdir"] == NO) {
- NSLog(@"change directory failed");
- }
- //返回当前路径
- path = [fm currentDirectoryPath];
- NSLog(@"Current directory path is %@",path);
- //删除目录
- if ([fm removeItemAtPath:path error:NULL] == NO) {
- NSLog(@"remove directory failed");
- }
- //枚举目录中的内容
- NSArray *dirArray;
- NSFileManager *fm = [[NSFileManager alloc]init];
- NSString *dirPath = [fm currentDirectoryPath]; //当前目录
- NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:dirPath]; //开始枚举过程,将其存入dirEnum中.
- //向dirEnum发送nextObject消息,返回下一个文件路径,当没有可供枚举时,返回nil.
- //enumeratorAtPath:方法会递归打印.
- NSString *file;
- while ((file = [dirEnum nextObject])) {
- if ([[file pathExtension] isEqualToString: @"doc"]){ //找出目录下面所有的doc文件
- NSString *fullPath = [dirPath stringByAppendingPathComponent:file];
- NSLog(@"%@",fullPath);
- }
- }
- dirArray = [fm contentsOfDirectoryAtPath:[fm currentDirectoryPath] error:NULL];
- NSLog(@"内容为:"); //使用contentsOfDirectoryAtPath:方法枚举当前路径中的文件并存入数组dirArray.
- for (NSString *path in dirArray){ //快速枚举数组中的内容并打印.
- NSLog(@"%@",path);
- }
- NSFileManager *fm = [[NSFileManager alloc]init];
- NSString *fName = @"path.m";
- NSString *upath = @"~s0s0/123/xxx/../321/./path.m";
- NSString *path, *tempdir, *extension, *homedir, *fullpath;
- NSArray *components;
- fm = [[NSFileManager alloc]init];
- //NSTemporaryDirectory()函数返回可以用来创建临时文件的目录路径名,如果要创建文件,完成任务后要删除;确保文件名是唯一的.
- tempdir = NSTemporaryDirectory();
- NSLog(@"临时文件夹路径为:%@",tempdir);
- path = [fm currentDirectoryPath]; //返回当前目录路径
- //lastPathComponent方法用与提取路径中最后一个目录名.
- NSLog(@"父目录名: %@",[path lastPathComponent]);
- //stringByAppendingPathComponent:方法将文件名插入到路径的末尾,这样就能显示一个文件的完整路径.
- fullpath = [path stringByAppendingPathComponent:fName];
- NSLog(@"完整路径为:%@ to %@",fName,fullpath);
- //pathExtension方法返回一个完整路径中的文件扩展名,如果没有扩展名,就返回空字符.
- extension = [fullpath pathExtension];
- NSLog(@"extension for %@ is %@ ",fullpath, extension);
- //NSHomeDirectory()函数返回当前用户的主目录
- //NSHomeDirectoryForUser(username)函数可以提供用户名做参数,并返回主目录名
- homedir = NSHomeDirectory();
- NSLog(@"主目录为 : %@",homedir);
- //pathComponents方法返回一个数组,数组中包含一个路径中的每个组成部分.
- components = [homedir pathComponents];
- for (path in components){
- NSLog(@"%@",path); //依次输出数组components中保存的元素.
- }
- //stringByStandardizingPath方法将原路径中的代字符转化为完整路径.
- //如果是路径名字中出现代字符,可以使用stringByExpandingTildeInPath方法.
- NSLog(@"%@ -> %@",upath, [upath stringByStandardizingPath]);
iOS - 操作文件目录的方法的更多相关文章
- [C#] Linq To Objects - 如何操作文件目录
Linq To Objects - 如何操作文件目录 开篇语: 上次发布的 <LINQ:进阶 - LINQ 标准查询操作概述> 社会反响不错,但自己却始终觉得缺点什么!“纸上得来终觉浅,绝 ...
- iOS-提高iOS开发效率的方法和工具
提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时 ...
- iOS开发——实用篇&提高iOS开发效率的方法和工具
提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时 ...
- iOS与HTML5交互方法总结(转)
今天小编在找技术文章的时候,发现这样一个标题:iOS与HTML5交互方法总结,怎么看着这么熟悉呢? 还以为是刚哥用了别的文章,点进去一看,原来是刚哥自己写的文章,他们转载的,而且还上了Dev St ...
- iOS UISearchController 的使用方法
iOS UISearchController 的使用方法 UISearchController 让用户在 UISearchBar 上输入搜索关键词,展示搜索结果或者进行其他操作.UISearchCon ...
- iOS与HTML5交互方法总结(修正)
摘要 看了不少别人写的博客或者论坛,关于iOS与HTML5交互方法大概主要有5种方式: 1. 利用WKWebView进行交互(系统API) 2. 利用UIWebView进行交互(系统API) 3. 苹 ...
- 一些ES5的操作数组的方法
在ES5规范中新增了不少操作数组的方法,特此罗列一下以备使用 1. forEach循环 有点类似jQuery的each循环 [12,23,36,4,5].forEach(function(v,k){ ...
- JavaScript操作JSON的方法总结,JSON字符串转换为JSON对象
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- android操作线程各种方法解析
(一)刚开始学习android的时候我是这么写的 new Thread( new Runnable() { public void run() { myView.invalidate(); } }). ...
随机推荐
- Aizu 2325 Mysterious Maze
走迷宫 ~ 不同的是题目给了你转向的方向序列 dis[x][y]表示到(x,y) 使用了最少的转向次数 #include<cstdio> #include<cstring> # ...
- 自定义nagios check_load告警阀值
自定义nagios check_load告警阀值 日期:2012-01-11 来源: heipark 分享至: - 默认check_load配置 define service{ use generi ...
- 1> Strut2 Mapping to MVC
CONTROLLER—FILTERDISPATCHER We’ll start with the controller. It seems to make more sense to start th ...
- POJ 3421
X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5111 Accepted: 1622 D ...
- Javascript 电子时钟源码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- POJ 3318 Matrix Multiplication(矩阵乘法)
题目链接 题意 : 给你三个n维矩阵,让你判断A*B是否等于C. 思路 :优化将二维转化成一维的.随机生成一个一维向量d,使得A*(B*d)=C*d,多次生成多次测试即可使错误概率大大减小. #inc ...
- Oracle 6 - 锁和闩 - 锁类型
Oracle锁大类 1.DML锁 (SELECT, INSERT, UPDATE, DELETE, MERGE是对数据库加的锁, 可能是行锁,也可能是表锁) 2.DDL锁 (Create, Alter ...
- 使用post()方法以POST方式从服务器发送数据
使用post()方法以POST方式从服务器发送数据 与get()方法相比,post()方法多用于以POST方式向服务器发送数据,服务器接收到数据之后,进行处理,并将处理结果返回页面,调用格式如下: $ ...
- 控制CPU占用率曲线
编程之美的第一个问题,我的机器是双核的,用文中的代码,得到的曲线波动比较大额,受其他进程的影响比较大.文中提到10ms接近windows的调度时间片,如果选得太小,会造成线程被频繁唤醒和挂起,造成内核 ...
- linux驱动学习之tasklet分析
tasklet是中断处理下半部分最常用的一种方法,驱动程序一般先申请中断,在中断处理函数内完成中断上半部分的工作后调用tasklet.tasklet有如下特点: 1.tasklet只可以在一个CPU上 ...