iphone 文件操作
2012-11-03 22:37
225 查看
1. 删除Document下的文件:
2. 从网上下载图片再存到document中:
3. 利用NSFileManager获取文件(文件夹)列表
在开发iPhone程序时,有时候要对文件进行一些操作。而获取某一个目录中的所有文件列表,是基本操作之一。通过下面这段代码,就可以获取一个目录内的文件及文件夹列表.
以下这段代码则可以列出给定一个文件夹里的所有子文件夹名
转载自:http://jinkeu.blog.163.com/blog/static/2089212920110293340721/
NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:self.photoObject.photoPath error:NULL];
2. 从网上下载图片再存到document中:
NSURL *url = [NSURL URLWithString:carImagePath]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [UIImage imageWithData:data]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *imagePath1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%f.png",[[NSDate date] timeIntervalSince1970]]]; NSData *imageData1 = UIImagePNGRepresentation(img); [imageData1 writeToFile:imagePath1 atomically:NO]; NSLog(@"imagePath1:%@",imagePath1);
3. 利用NSFileManager获取文件(文件夹)列表
在开发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);
转载自:http://jinkeu.blog.163.com/blog/static/2089212920110293340721/
相关文章推荐
- Iphone文件操作和字符串操作的工具类
- iphone 详解iPhone文件读写系统操作教程
- 【IPHONE开发-OBJECTC入门学习】文件的操作,读写复制文件
- iPhone 文件结构和文件操作
- iPhone 文件读写操作
- iphone ios 文件系统操作(创建、删除,修改,获取属性)
- IOS/iphone开发文件操作
- iphone 文件操作以及文件管理
- iPhone 文件结构和文件操作
- iPhone文件操作
- iPhone App 文件操作
- iphone-文件操作初步理解
- Iphone文件读写操作
- iphone 文件操作
- iPhone 文件结构和文件操作
- Iphone文件操作和字符串操作的工具类
- iphone 文件操作以及文件管理 2
- iphone-文件操作初步理解
- iPhone文件操作
- iPhone 文件结构和文件操作