Iphone 读取 指定 文件 内容(document)
2011-12-07 08:56
204 查看
1原文摘自:http://ruixiazun.blog.163.com/blog/static/906879182011229112144105/
在开发iPhone程序时,有时候要对文件进行一些操作。而获取某一个目录中的所有文件列表,是基本操作之一。通过下面这段代码,就可以获取一个目录内的文件及文件夹列表。NSFileManager *fileManager = [NSFileManager defaultManager];
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
以下这段代码则可以列出给定一个文件夹里的所有子文件夹名
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
BOOL isDir = NO;
//在上面那段程序中获得的fileList中列出文件夹名
for (NSString *file in fileList) {
NSString *path = [documentDir stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if (isDir) {
[dirArray addObject:file];
}
isDir = NO;
}
NSLog(@"Every Thing in the dir:%@",fileList);
NSLog(@"All folders:%@",dirArray);
在开发iPhone程序时,有时候要对文件进行一些操作。而获取某一个目录中的所有文件列表,是基本操作之一。通过下面这段代码,就可以获取一个目录内的文件及文件夹列表。NSFileManager *fileManager = [NSFileManager defaultManager];
//在这里获取应用程序Documents文件夹里的文件及文件夹列表
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
以下这段代码则可以列出给定一个文件夹里的所有子文件夹名
NSMutableArray *dirArray = [[NSMutableArray alloc] init];
BOOL isDir = NO;
//在上面那段程序中获得的fileList中列出文件夹名
for (NSString *file in fileList) {
NSString *path = [documentDir stringByAppendingPathComponent:file];
[fileManager fileExistsAtPath:path isDirectory:(&isDir)];
if (isDir) {
[dirArray addObject:file];
}
isDir = NO;
}
NSLog(@"Every Thing in the dir:%@",fileList);
NSLog(@"All folders:%@",dirArray);
相关文章推荐
- iphone存储以及读取指定文件(Documents)中的内容
- iphone存储以及读取指定文件(Documents)中的内容
- iphone存储以及读取指定文件(Documents)中的内容
- iphone存储以及读取指定文件(Documents)中的内容
- 生成文件列表并读取列表中各个文件的指定内容(C语言)
- C/C++ 读取文件指定行的内容
- java按指定编码写入和读取文件内容的类
- C# 从内嵌资源读取文件内容并写入到指定位置
- java按指定编码写入和读取文件内容的类
- 使用Python将TXT文本内容读取后生成指定XML格式的文件
- 简单的读取文件内容并写到另一个指定的文件中
- java实现读取指定文件内容
- iphone 读取 一行文件内容的两种方法
- 读取指定URL连接的文件内容
- Java 小例子:按指定的编码读取文本文件内容
- java按指定编码写入和读取文件内容的
- QT QDomDocument读取XML文件内容
- 在文件指定位置读取和写入内容RandomAccessFile
- c++读取文件内容并统计指定字符的个数
- java按指定编码写入和读取文件内容的类