NSDirectoryEnumerator
来源:互联网 发布:免流软件原理 编辑:程序博客网 时间:2024/06/07 09:40
需要获得目录的内容列表,使用enumeratorAtPath:方法或者directoryC ontentsAtPath:方法,可以完成枚举过程。
http://blog.csdn.net/lklmr/article/details/42713021
如果使用第一种enumeratorAtPath:方法,一次可以枚举指定目录中的每个文件。默认情况下,如果其中一个文件为目录,那么也会递归枚举它的内容。在这个过程中,通过向枚举对象发送一条skipDescendants消息,可以动态地阻止递归过程,从而不再枚举目录中的内容。
对于directoryContentsAtPath:方法,使用这个方法,可以枚举指定目录的内容,并在一个数组中返回文件列表。如果这个目录中的任何文件本身是个目录,这个方法并不递归枚举它的内容。
代码如下:
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSString *path;
- NSFileManager *fm;
- NSDirectoryEnumerator *dirEnum;
- NSArray *dirArray;
- fm = [NSFileManager defaultManager];
- //获取当前的工作目录的路径
- path = [fm currentDirectoryPath];
- //遍历这个目录的第一种方法:(深度遍历,会递归枚举它的内容)
- dirEnum = [fm enumeratorAtPath:path];
- NSLog(@"1.Contents of %@:",path);
- while ((path = [dirEnum nextObject]) != nil)
- {
- NSLog(@"%@",path);
- }
- //遍历目录的另一种方法:(不递归枚举文件夹种的内容)
- dirArray = [fm directoryContentsAtPath:[fm currentDirectoryPath]];
- NSLog(@"2.Contents using directoryContentsAtPath:");
- for(path in dirArray)
- NSLog(@"%@",path);
- }
- return 0;
- }
通过以上程序变例如下文件路径:
可以得到如下结果:
如果对上述代码while循环做如下修改,可以阻止任何子目录中的枚举:
- while ((path = [dirEnum nextObject]) != nil)
- {
- NSLog(@"%@",path);
- BOOL flag;
- [fm fileExistsAtPath:path isDirectory:&flag];
- if(flag == YES)
- [dirEnum skipDescendants];
- }
这里flag是一个BOOL类型的变量。如果指定的路径是目录,则fileExistsAtPath:在flag中存储YES,否则存储NO。
另外提醒下,无须像在这个程序中那样进行快速枚举,使用以下NSLog调用也可以显示所有的dirArray的内容:
- NSLog(@"%@",dirArray);
转:http://blog.csdn.net/hekunhotmail/article/details/8725750
iOS 获取文件属性 NSDirectoryEnumerator 文件大小
//如果没有目录则创建目录
[[NSFileManager defaultManager]createDirectoryAtPath:[kDocumentsPathstringByAppendingPathComponent:@"PhotoImg"] withIntermediateDirectories:YES attributes:nil error:nil];
//递归枚举目录
NSString *dir = [NSString stringWithFormat:@"%@/PhotoImg",kDocumentsPath];
NSDirectoryEnumerator *dirEnumerater = [[NSFileManager defaultManager] enumeratorAtPath:dir];
NSString *filePath = nil;
NSDictionary *arrtibutes = [[NSFileManager defaultManager]attributesOfItemAtPath:dir error:nil];
NSNumber *theFileSize ;
if ((theFileSize = [arrtibutes objectForKey:NSFileSize])) {
NSLog(@"%@",theFileSize);
}
NSLog(@"%@",[dirEnumerater fileAttributes]);
int fileSizeReal = getFileSizeFormPath((char *)[dir UTF8String]);
NSLog(@"%d",fileSizeReal);
int getFileSizeFormPath(char *path){
FILE * file;
int fileSizeByte = 0;
file = fopen(path, "r");
if (file>0) {
fseek(file, 0, SEEK_END);
fileSizeByte = ftell(file);
fseek(file, 0, SEEK_SET);
fclose(file);
}
return fileSizeByte;
}
- NSDirectoryEnumerator
- NSDirectoryEnumerator
- iOS 获取文件属性 NSDirectoryEnumerator 文件大小
- ios开发系列之使用NSDirectoryEnumerator的对象获得目录的资源列表
- PopUpWindow使用详解(一)——基本使用
- IOS中根视图控制器的剖析
- 马虎的算式(穷举暴破)
- shell订时检测sshd的端口
- 蓝桥杯 未名湖边的烦恼
- NSDirectoryEnumerator
- [matlab]在cvx中使用Mosek
- 线性表之静态链表
- 二进制中1的个数
- 解决 eclipse 启动报 ”failed to load the jni shared library“ 问题
- 【POJ 1195】Mobile phones
- 练习轮播海报
- 汉诺塔解法C语言实现
- Android子线程中更新UI的3种方法